简体   繁体   English

通过一次查询选择不同行上的多个字段

[英]Select multiple fields on different rows with ONE query

With one MYSQL query only, how can I check if a buyer and a seller have both bought from each other ? 仅使用一个MYSQL查询,如何检查买卖双方是否都相互购买了商品?

Table : purchase 表:购买

purchase_id     |   seller       |   buyer
       12       |      2         |     5
       29       |      5         |     2

If the query returns a result (which would be the case here), then a specific div would appear on the page. 如果查询返回结果(此处就是这种情况),则特定的div将出现在页面上。

SELECT (COUNT(*) >= 2) AS Result
FROM   purchase a
       INNER JOIN purchase b
         ON a.seller = b.buyer AND
            a.buyer = b.seller
WHERE  (a.seller = 2 AND a.buyer = 5) OR
       (a.seller = 5 AND a.buyer = 2)

just replace 2 and 5 with your variables. 只需将25替换为您的变量即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM