Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Using joins in single query question

$
0
0
Hello everyone, I'm new to this forum. I used to learn SQL at school but it's quite hazy now since I havent used it in a long time since I started working.Anyways, for work, I'm making a database of our customers. I have two tables: tblCustomerstblContactstblCustomers contains the customers and for the sake of simplicity, lets say it has these columns:- Name- Accountnumber- ID (auto increment) to be sure each record can be uniquely identifiedtblContacts contains the contact persons at the customers (for example, the CEO, the IT-man, the accountant for company X)- Name- Email- CustomerID: this refers to the auto increment ID column in tblCustomers so we now which customer it belongs toWhen I do a search for a customer, it must be possible to be able to extract all contacts that belong to it but we don't need to be able to search for a contact to see which customer it belongs to.It's always Search customer --> Which contacts belong to him.However, I'm not quite sure how to insert new records in the database properly. I have a php page with one single form. This php contains variables for each one of the fields of both tables- customername- contactname- customeraccount- contactemailI remember how you do an insert. [b]INSERT INTO tblCustomers (Name, Accountnumber)VALUES (customername, customeraccount);[/b]and[b]INSERT INTO tblContacts (Name, Email)VALUES (contactname, contactemail);[/b]So far it's quite simple right?However... the problem arises when I want to make sure the record in tblContacts is properly linked to it's corresponding record in tblCustomers. How can I give in the value CustomerID of the tblContacts table the value that was assigned to this customer when creating the record in tblCustomers at the same time?I vaguely remember some stuff about joins, but there's so many different types... you don't have to give me the specific code, but if you can point me in the good direction, I'll use good ol' Google to do the rest :-)Thanks!

Viewing all articles
Browse latest Browse all 3145

Trending Articles