加入两个选择语句

Posted

技术标签:

【中文标题】加入两个选择语句【英文标题】:joining two select statements 【发布时间】:2011-08-27 12:18:23 【问题描述】:

谁能告诉我为什么以下不起作用?它抱怨两个选择之间的连接关键字附近的语法错误。

SELECT * 
FROM ( select * from orders_products inner JOIN orders ON orders_products.orders_id = orders.orders_id  where products_id = 181) 
as A

join 

SELECT * 
FROM ( select * from orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id  where products_id = 180) 
as B

on A.orders_id=B.orders_id

基本上我的第一个SELECT 从一个表中提取某个产品的所有订单信息,然后从另一个表中提取订购的数量并将它们连接在一起。第二个SELECT 对另一个产品做同样的事情。

现在,我有

_______A_________ _______B_________ O_ID P_ID Q O_ID P_ID Q 1 180 3 1 181 11 2 180 9 2 181 6 3 180 5 3 181 3

而且,使用另一个加入我想得到 ​​p>

Q_ID P_ID1 Q1 P_ID2 Q2
1 180 3 181 11
2 180 9 181 6
3 180 5 181 3

也许我在这里采取了错误的方法。有什么建议吗?

更新: 在 RedFilter 的指示之后,这对我有用:

(SELECT * 
FROM (
SELECT * FROM orders_products
INNER JOIN orders ON orders_products.orders_id = orders.orders_id
WHERE products_id =181) AS A
LEFT JOIN (
SELECT * FROM orders_products
INNER JOIN orders ON orders_products.orders_id = orders.orders_id
WHERE products_id =180) AS B ON A.orders_id = B.orders_id
)
UNION (
SELECT * 
FROM (
SELECT * 
FROM orders_products
INNER JOIN orders ON orders_products.orders_id = orders.orders_id
WHERE products_id =181
) AS C
RIGHT JOIN (
SELECT * 
FROM orders_products
INNER JOIN orders ON orders_products.orders_id = orders.orders_id
WHERE products_id =180
) AS D ON C.orders_id = D.orders_id
) 

【问题讨论】:

嗯,所有的加入看起来都没有必要...... 你能描述一下你想从这个查询中检索什么吗?可能有更有效的方法。 【参考方案1】:

不确定您要做什么,但您有两个选择子句。改为这样做:

SELECT * 
FROM ( SELECT * 
       FROM orders_products 
       INNER JOIN orders ON orders_products.orders_id = orders.orders_id 
       WHERE products_id = 181) AS A
JOIN ( SELECT * 
       FROM orders_products 
       INNER JOIN orders ON orders_products.orders_id = orders.orders_id
       WHERE products_id = 180) AS B

ON A.orders_id=B.orders_id

更新:

你可以把它简化成这样:

SELECT o.orders_id, 
       op1.products_id, 
       op1.quantity, 
       op2.products_id, 
       op2.quantity
FROM orders o
INNER JOIN orders_products op1 on o.orders_id = op1.orders_id  
INNER JOIN orders_products op2 on o.orders_id = op2.orders_id  
WHERE op1.products_id = 180
AND op2.products_id = 181

【讨论】:

您应该仔细检查您的答案,这正是 OP 在他的问题中发布的内容。 @maple_shaft 可能看起来是这样,但我在JOIN 子句之后删除了SELECT * FROM 就成功了。抱歉,我的问题一开始并不清楚,但是当我添加更多细节时,你们发布了答案,结果证明这是我想要的。 您实际上知道,我认为我需要完全加入来实现我的目标,如果我在原始解决方案中替换为“完全加入”而不是“加入”,我会收到错误消息。你对如何解决它有什么想法吗?基本上我需要在结果集中查看我的所有订单,即使订单中只有一种产品。 我的问题通过使用这些多个内连接解决了,对我来说真的很好。【参考方案2】:

如果你想组合不同的结果集,你应该使用UNION。请尝试以下操作:

(SELECT * 
 FROM ( SELECT * 
        FROM orders_products 
        INNER JOIN orders ON orders_products.orders_id = orders.orders_id  
        WHERE products_id = 181) AS A)
UNION 

(SELECT * 
 FROM ( SELECT * 
        FROM orders_products 
        INNER JOIN orders ON orders_products.orders_id = orders.orders_id 
        WHERE products_id = 180) AS B
ON A.orders_id=B.orders_id)

【讨论】:

【参考方案3】:

这会做你想做的事:

select * 
  from orders_products 
       INNER JOIN orders 
          ON orders_products.orders_id = orders.orders_id
 where products_id in (180, 181);

【讨论】:

【参考方案4】:
SELECT *
FROM
  (First_query) AS ONE
LEFT OUTER JOIN
  (Second_query ) AS TWO ON ONE.First_query_ID = TWO.Second_Query_ID;

【讨论】:

请在您的答案中添加探索 SELECT * FROM (SELECT c.name AS COUNTRY_NAME FROM STATS_TABLE ebs INNER JOIN CLASS_TABLE ebsec ON ebs.ID = ebsec.STATS_TABLE_id AND ebs.USER_ID = ebsec.user_id INNER JOIN classes ec ON ebsec.class_id = ec.id WHERE ebs.created_at blabla GROUP BY es.id) AS ea LEFT OUTER JOIN (SELECT es.School_id FROM STATS_TABLE ebs INNER JOIN CLASS_TABLE ebsec ON ebs.ID = ebsec.STATS_TABLE_id AND USER_ID = ebsec.user_id WHERE ebs.STATUS IN ('COMPLETED') AND ebs.creted GROUP BY es.id) AS ear ON ea.School_id = ear.School_id;

以上是关于加入两个选择语句的主要内容,如果未能解决你的问题,请参考以下文章

使用 case 语句从两个不同的表中选择数据

你如何加入两个计算的sql语句?

选择加入2个表和两个表的最新记录

MySQL:将子选择和数组合并到一个IN语句中

加入两个表但没有结果

尝试在两个 with 语句之后使用选择查询