2016-03-08 68 views
0

我的表:MySQL的內部三個表之間的連接語法

mysql> select * from pvf_order; 
+------+------------+------+ 
| oid | orderdate | cid | 
+------+------------+------+ 
| 1001 | 2014-02-10 | 1 | 

10 rows in set (0.00 sec) 

mysql> select * from pvf_order_item; 
+------+-----+----------+ 
| oid | pid | quantity | 
+------+-----+----------+ 
| 1001 | 1 |  2 | 

18 rows in set (0.00 sec) 

mysql> select * from pvf_product; 
+-----+----------------------+----------------+--------+--------------+ 
| pid | prod_desc   | Prod_finish | price | prod_line_id | 
+-----+----------------------+----------------+--------+--------------+ 
| 1 | End Table   | Cherry   | 175.00 |   1 | 

8 rows in set (0.01 sec) 

的現在我的查詢變化:

SELECT 
pvf_order.cid, 
pvf_order.orderdate, 
pvf_product.prod_desc, 
pvf_order_item.quantity, 
pvf_product.price 
FROM pvf_order 
INNER JOIN pvf_order.oid=pvf_order_item.oid 
INNER JOIN pvf_order_item.pid=pvf_product.pid 
WHERE YEAR(pvf_order.orderdate)=2014 AND MONTH(pvf_order.orderdate)=10; 

我只是不知道到底我要去哪裏錯了。我編輯了一些信息,以避免大量文字,但我希望總體思路在那裏。任何幫助表示讚賞!

+0

你需要有這些變化:'INNER JOIN pvf_order_item ON pvf_order.oid = pvf_order_item.oid'和'INNER JOIN pvf_product ON pvf_order_item.pid = pvf_product.pid' – 1000111

+0

它應該是'INNER JOIN pvf_order_item ON pvf_order .oid = pvf_order_item.oid INNER JOIN pvf_product ON pvf_order_item.pid = pvf_product.pid'您需要提及要連接的表格,而不僅僅是要匹配的字段。 –

+0

真棒謝謝! – Burninrock24

回答

0

檢查JOIN sintaxis,還使用ALIAS的表名

SELECT 
    o.cid, 
    o.orderdate, 
    p.prod_desc, 
    oi.quantity, 
    p.price 
FROM pvf_order o 
INNER JOIN pvf_order_item oi 
     ON o.oid = oi.oid 
INNER JOIN pvf_product p 
     ON pvf_o.pid = p.pid 
WHERE YEAR(o.orderdate) = 2014 AND MONTH(o.orderdate) = 10; 

而且改用年和月功能,可以簡化爲一個單一的比較討論操作。

WHERE date_format(o.orderdate, '%Y-%m') = '2014-10'