Forums
This topic is locked
SQL to include data from 3 tables
22 Oct 2002 17:51:00 mark capa posted:
<font face='Arial'>Below is a SQL statement that merges the Customer table and Order Details tables. I'm wanting to introduce a column from a third table, but not sure how to do this. Any suggestions?
SELECT orderid, productid, unitprice, quantity, customer_id, contact_name, email_address, address_line1, address_line2, city, county, post_zip_code, country, phone
FROM orderdetails, customers
WHERE orderdetails.orderid = customers.customer_id</font id='Arial'>
www.gwenap.net
Spice Up Your Sex Life!
Replies
Replied 23 Oct 2002 13:01:26
23 Oct 2002 13:01:26 Julio Taylor replied:
what's the foreign key of the third table?
------------------------
Julio Mellado
PHP | MySQL | UD4
ICQ: 19735247
MSN:
------------------------
Julio Mellado
PHP | MySQL | UD4
ICQ: 19735247
MSN:
Replied 23 Oct 2002 13:08:18
23 Oct 2002 13:08:18 mark capa replied:
<font face='Arial'>The third table is named "Orders" and the common key is orderid, the column I wish to display additional to the rest is "order_date"
Hope this helps.</font id='Arial'>
www.gwenap.net
Free Vibrators to European countries.
Spice Up Your Sex Life!
Hope this helps.</font id='Arial'>
www.gwenap.net
Free Vibrators to European countries.
Spice Up Your Sex Life!
Replied 24 Oct 2002 10:19:07
24 Oct 2002 10:19:07 Julio Taylor replied:
ah, in that case it should be easy (changes in bold)
<pre id=code><font face=courier size=2 id=code>
SELECT orderid, productid, unitprice, quantity, customer_id, contact_name, email_address, address_line1, address_line2, city, county, post_zip_code, country, phone, <b>orders.order_date</b>
FROM orderdetails, customers, <b>orders</b>
WHERE orderdetails.orderid = customers.customer_id <b>AND orderdetails.orderid = orders.orderid</b>
</font id=code></pre id=code>
This should include the data from the 3rd field. however you'll need to add an <i>GROUP BY</i> clause in there too, but it depends what field is going to be the primary key in this query. i'm guessing it will be <i>orderdetails.orderid</i>. In this case:
<pre id=code><font face=courier size=2 id=code>
SELECT orderid, productid, unitprice, quantity, customer_id, contact_name, email_address, address_line1, address_line2, city, county, post_zip_code, country, phone, <b>orders.order_date</b>
FROM orderdetails, customers, <b>orders</b>
WHERE orderdetails.orderid = customers.customer_id <b>AND orderdetails.orderid = orders.orderid
GROUP BY orderdetails.orderid ASC</b>
</font id=code></pre id=code>
please let me know if this works or if you need any more help at all...
-- Julio
------------------------
Julio Mellado
PHP | MySQL | UD4
ICQ: 19735247
MSN:
<pre id=code><font face=courier size=2 id=code>
SELECT orderid, productid, unitprice, quantity, customer_id, contact_name, email_address, address_line1, address_line2, city, county, post_zip_code, country, phone, <b>orders.order_date</b>
FROM orderdetails, customers, <b>orders</b>
WHERE orderdetails.orderid = customers.customer_id <b>AND orderdetails.orderid = orders.orderid</b>
</font id=code></pre id=code>
This should include the data from the 3rd field. however you'll need to add an <i>GROUP BY</i> clause in there too, but it depends what field is going to be the primary key in this query. i'm guessing it will be <i>orderdetails.orderid</i>. In this case:
<pre id=code><font face=courier size=2 id=code>
SELECT orderid, productid, unitprice, quantity, customer_id, contact_name, email_address, address_line1, address_line2, city, county, post_zip_code, country, phone, <b>orders.order_date</b>
FROM orderdetails, customers, <b>orders</b>
WHERE orderdetails.orderid = customers.customer_id <b>AND orderdetails.orderid = orders.orderid
GROUP BY orderdetails.orderid ASC</b>
</font id=code></pre id=code>
please let me know if this works or if you need any more help at all...
-- Julio
------------------------
Julio Mellado
PHP | MySQL | UD4
ICQ: 19735247
MSN: