简体   繁体   English

Mysql选择只有特定条件的结果

[英]Mysql select results that have only a specific condition

here is my problem: I have a list of users that are associated with different international projects, I want to get the users that are working exclusively for US projects, so let's say if someone has worked for a German project, I don't want him in the list. 这是我的问题:我有一个与不同国际项目相关的用户列表,我希望得到专门为美国项目工作的用户,所以让我们说如果有人为德国项目工作,我不想要他在名单中。 It involves three tables: users, projects, company 它涉及三个表:用户,项目,公司

Here is the first query to get users that are working for US projects: 这是第一个获取用于美国项目的用户的查询:

SELECT DISTINCT users.forename, users.surname, users.email, users.company_id,  users.user_id
FROM projects, company, users
WHERE projects.project_type = 'US'
AND company.project_id = projects.project_id
AND users.company_id = company.company_id
ORDER BY surname, forename

Now how can I exclude users that have also other countries' projects? 现在我如何排除还有其他国家项目的用户?

You can use NOT IN clause to exclude user that work on other project. 您可以使用NOT IN子句来排除在其他项目上工作的用户。

In where condition add below line 在条件添加到下面的行

WHERE projects.user_id 
  NOT IN (select user_id from projects where projects.project_type != 'US')

You can join your tables and filter with the condition 您可以加入表格并根据条件进行过滤

SELECT DISTINCT users.forename, users.surname, users.email, users.company_id,  users.user_id
FROM users
JOIN company USING (company_id)
JOIN projects USING (project_id)
WHERE projects.project_type = 'US'
ORDER BY surname, forename

this query creates rows with users and information about the company and the project and after filter these rows with the condition WHERE projects.project_type = 'US' 此查询创建包含用户的行以及有关公司和项目的信息,并在使用条件WHERE projects.project_type = 'US'过滤这些行之后

You can try with HAVING . 你可以试试HAVING If user worked only in US then COUNT need to be 1: 如果用户仅在美国工作,则COUNT必须为1:

SELECT DISTINCT users.forename, COUNT(projects.*) AS c, users.surname, users.email, users.company_id,  users.user_id
FROM projects, company, users
WHERE projects.project_type = 'US'
AND company.project_id = projects.project_id
AND users.company_id = company.company_id
GROUP BY projects.project_type
HAVING c = 1
ORDER BY surname, forename

Maybe this code doesn't work, can't check but you can try in this direction. 也许这段代码不起作用,无法检查,但你可以尝试这个方向。

A not exists would do the job not exists会做这个工作

SELECT DISTINCT users.forename, users.surname, users.email, users.company_id,  users.user_id
FROM projects, company, users
WHERE projects.project_type = 'US'
AND company.project_id = projects.project_id
AND users.company_id = company.company_id
AND NOT EXISTS (
    SELECT * 
      FROM projects notUS 
     WHERE notUS.project_type <> 'US' 
       AND notUS.user_id = users.user_id)
ORDER BY surname, forename

You can use this:- 你可以用这个: -

SELECT DISTINCT users.forename, users.surname, users.email, users.company_id,  users.user_id
FROM projects, company, users
WHERE users.company_id = company.company_id
AND company.project_id = projects.project_id
AND projects.user_id 
IN (select user_id from projects where projects.project_type = 'US')
ORDER BY surname, forename

Try this i think you need MINUS: 试试这个我觉得你需要MINUS:

SELECT 
    users.forename, users.surname, users.email, users.company_id,  users.user_id
FROM 
    users
where 
    user_id not in(SELECT DISTINCT user_id from users where company_id in
    (
        Select company_id from company where project_id = (
            Select project_id from projects where project_type != 'US'
        )
    ) 

You can do it like this too 你也可以这样做

SELECT DISTINCT
  users.forename,
  users.surname,
  users.email,
  users.company_id,
  users.user_id
FROM projects
  INNER JOIN company
    ON company.project_id = projects.project_id
  INNER JOIN users
    ON users.company_id = company.company_id
  INNER JOIN (SELECT *
          FROM projects
          WHERE project_type <> 'US') AS project
    ON project.project_id = projects.project_id
WHERE project.project_id IN NULL
GROUP BY projects.project_id
ORDER BY surname, forename

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

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