带有select和grouping

时间:2017-02-17 11:38:54

标签: mysql

我想选择10个提交的所有客户记录,而不是选择20个作为代表的客户记录。所以我的愿望结果是为客户2和3选择了2个总记录.4被排除,因为20是该集合的一部分。非常感谢任何帮助。

+--------------+--------+----------------+
| Submitted_By | Client | Representative |
+--------------+--------+----------------+
|           10 |      2 |             15 |
|           10 |      2 |             16 |
|           10 |      2 |             17 |
|           10 |      3 |             15 |
|           10 |      3 |             16 |
|           10 |      3 |             19 |
|           10 |      4 |             15 |
|           10 |      4 |             16 |
|           10 |      4 |             17 |
|           10 |      4 |             20 |
+--------------+--------+----------------+

尝试

SELECT
    Client,
    Submitted_By,
    Representative,
FROM
    client
WHERE
Client = 10 AND Representative != 20

1 个答案:

答案 0 :(得分:0)

您可以使用以下子查询:

SELECT Client, Submitted_By, Representative 
   FROM client 
   WHERE Submitted_By = 10 
   AND Client NOT IN (SELECT Client FROM client WHERE Representative = 20)

你写的只想要" 2总记录"回。然后是GROUP BY Client statement可用于从您提供的示例表中返回两行,每个客户端一行。