从4个不同的表中选择多个列

时间:2014-02-28 03:36:11

标签: sql select inner-join multiple-tables multiple-select

我有4个表:order,order_product,product,product_description。

我试图从这四个表中获得以下colomuns: 作者(product_description),名称(product_description),型号(产品), order_id (订单) 在order.order_status_id = 1

的时候

我正在使用此查询:

SELECT          product_description.name AS name
        product_description.author AS author
        product.model AS model
            `order`.`order_id` AS `order_id`
FROM product_description
INNER JOIN product_description ON product.product_id=product_descripton.product_id
INNER JOIN product ON `order`.product_id=product.product_id
INNER JOIN `order` ON `order_product`.`order_id`=`order`.`order_id`
WHERE `order`.`order_status_id`=1

但它不起作用。 有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

你忘记了select中的逗号并且你加入了同一个表两次,并且你在其中一个内连接中有一个拼写错误,这应该有效:

SELECT  product_description.name AS name,
        product_description.author AS author,
        product.model AS model,
        `order`.`order_id` AS `order_id`
FROM product_description
INNER JOIN product ON product_description.product_id=product.product_id
INNER JOIN order_product ON order_product.product_id=product.product_id
INNER JOIN `order` ON `order`.order_id=order_product.order_id
WHERE `order`.order_status_id=1