Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 correct query syntax

Author  Topic 

maineman
Starting Member

7 Posts

Posted - 2012-01-31 : 15:29:02
Hi,

Im having trouble connecting the following tables:

Item
+-------------+--------------+------+-----+----------------+
| Field | Type | Null | Key | Extra |
+-------------+--------------+------+-----+----------------+
| itemID | int(11) | | PRI | auto_increment |
| name | varchar(64) | | | |
| description | varchar(128) | YES | | |
| categoryID | int(11) | YES | | |
+-------------+--------------+------+-----+----------------+

Category
+-------------+--------------+------+-----+----------------+
| Field | Type | Null | Key | Extra |
+-------------+--------------+------+-----+----------------+
| categoryID | int(11) | | PRI | auto_increment |
| name | varchar(64) | | | |
+-------------+--------------+------+-----+----------------+

ItemOrderMembership
+-------------+--------------+------+-----+----------------+
| Field | Type | Null | Key | Extra |
+-------------+--------------+------+-----+----------------+
| orderID | int(11) | | | |
| itemID | item(11) | | | |
+-------------+--------------+------+-----+----------------+

I need the orderID from ItemOrderMembership but not sure how to join the tables correctly. Can anyone show me? Thanks

maineman
Starting Member

7 Posts

Posted - 2012-01-31 : 15:48:15
In other words how would you write a query to join these tables?
Go to Top of Page

maineman
Starting Member

7 Posts

Posted - 2012-01-31 : 16:40:07
What I need is a query that will give me the name from the item table and category table where the orderid = 12345 for example. Any help appreciated.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-31 : 16:44:55
its just a simple inner join among tables


SELECT i.name AS ItemName,c.name AS CategoryName
FROM ItemOrderMembership iom
INNER JOIN Item i
ON i.itemID = iom.itemID
INNER JOIN Category c
ON c.categoryID = i.categoryID
WHERE iom.OrderID = 12345


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -