MySQL内部连接三个表之间的语法

时间:2016-03-08 17:16:52

标签: 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;

我只是不确切地知道我哪里出错了。我编辑了一些信息只是为了避免一大堆文字,但我希望一般的想法就在那里。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

检查 JOIN sintaxis,同时使用 ALIAS 作为表名

Hostname    Last Modified
HD          9/8/2012
LOG         9/15/2004
NETMAN     12/25/2004
NETMAN      5/5/2015
LOG        2/14/2013
LOG        6/6/2011

此外,您可以简化为单一的比较操作,而不是使用年份和月份功能。

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;