3

这是我的问题:我有一个与不同国际项目相关的用户列表,我想获得专门为美国项目工作的用户,所以假设有人为德国项目工作,我不想要他在名单中。涉及三张表:users、projects、company

这是获取为美国项目工作的用户的第一个查询:

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

现在如何排除也有其他国家项目的用户?

4

7 回答 7

2

您可以使用 NOT IN 子句排除在其他项目上工作的用户。

在 where 条件下添加以下行

WHERE projects.user_id 
  NOT IN (select user_id from projects where projects.project_type != 'US')
于 2013-04-19T10:45:41.903 回答
1

您可以加入表格并使用条件进行过滤

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

此查询创建包含用户以及有关公司和项目的信息的行,并在使用条件过滤这些行之后WHERE projects.project_type = 'US'

于 2013-04-19T10:51:19.190 回答
1

您可以尝试使用HAVING。如果用户仅在美国工作,则 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

也许此代码不起作用,无法检查,但您可以朝这个方向尝试。

于 2013-04-19T10:51:41.520 回答
0

Anot 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
于 2013-04-19T10:50:22.377 回答
0

你可以使用这个: -

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
于 2013-04-19T10:51:59.387 回答
0

试试这个,我认为你需要 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'
        )
    ) 
于 2013-04-19T10:54:16.387 回答
0

你也可以这样做

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
于 2013-04-19T11:07:13.177 回答