MYSQL SELECT故障(列上的AND运算符)

时间:2014-10-21 05:37:28

标签: mysql

我有一张类似于以下内容的表格:

+----+------+----------+
| id | Order| SalesMan |
+----+------+----------+
| 1  | 1001 |     1    |
| 2  | 1001 |     2    |
| 3  | 1002 |     1    |
| 4  | 1002 |     4    |
| 5  | 1003 |     2    |
+----+------+----------+

我想选择与AND运算符共享多个Sales Men的订单。就像只显示由Salesmen 1&amp ;; 2。

3 个答案:

答案 0 :(得分:3)

根据我对您的问题的理解,将会有一个订单与多个销售人员相关联。如果您只需要了解那些与销售员1和2相关的订单,那么您可以这样做:

   SELECT `order` 
   FROM tbl 
   WHERE salesMan=1 
   AND `order` 
   IN (select `order` from tbl where salesMan=2)

这是SQL Fiddle以便更好地理解..

编辑:我已根据您的需要更改了查询a / c:

SELECT orders 
FROM tbl 
WHERE salesMan 
IN (1,2,3) 
GROUP BY orders 
HAVING count(orders)>1;

检查fiddle demo此处

答案 1 :(得分:0)

使用GROUP BYHAVING条款。

查询

SELECT salesMan
FROM tbl
GROUP BY salesMan
HAVING COUNT(salesMan) > 1;

Fiddle Demo

答案 2 :(得分:0)

试试这个

SELECT * FROM table WHERE SalesMan IN(1,2);