ms-access:从另一个查询中选择

时间:2009-08-31 18:04:38

标签: sql mysql ms-access

我正在连接对mysql数据库的访问。

我需要整理两个陈述并将它们合二为一。

例如:

SELECT 
  users.id,
  users.first,
  users.last,
  chavrusas.luser_type AS user_type,
  chavrusas.id, 
  users.title, 
  users.city, 
  users.state, 
  users.home_phone, 
  users.email
FROM users
INNER JOIN chavrusas 
  ON Users.id=chavrusas.luser_id
WHERE     ((chavrusas.ruser_id)='3166' and chavrusas.ended=false) 
  AND     (chavrusas.luser_type) <> (chavrusas.ruser_type)
  AND NOT ((chavrusas.luser_type)='teacher' AND (chavrusas.ruser_type)='student')
  AND NOT ((chavrusas.ruser_type)='teacher' AND (chavrusas.luser_type)='student'); 
UNION
SELECT  
  users.id, 
  users.first, 
  users.last, 
  chavrusas.ruser_type AS user_type, 
  chavrusas.id, 
  users.title, 
  users.city, 
  users.state, 
  users.home_phone, 
  users.email
FROM users
INNER JOIN chavrusas 
  ON Users.id=chavrusas.ruser_id
WHERE     ((chavrusas.luser_id)='3166' and chavrusas.ended=false)
  AND     (chavrusas.luser_type) <> (chavrusas.ruser_type)
  AND NOT ((chavrusas.luser_type)='teacher' AND (chavrusas.ruser_type)='student')
  AND NOT ((chavrusas.ruser_type)='teacher' AND (chavrusas.luser_type)='student')
ORDER BY 4;

用户是一个查询:

SELECT 
  tblusers.*,
  tblusershliach.*,
  tbluserstudent.*,
  tbluserstudentteacher.*,
  tbluserteacher.*
FROM
(
  (
    (tblusers 
     LEFT JOIN tblusershliach 
     ON tblusers.id = tblusershliach.shliach_user_id
    )
  LEFT JOIN tbluserstudent 
  ON tblusers.id = tbluserstudent.student_user_id
  )
LEFT JOIN tbluserstudentteacher
ON tblusers.id = tbluserstudentteacher.student_teacher_user_id
) 
LEFT JOIN tbluserteacher 
ON tblusers.id = tbluserteacher.teacher_user_id;

而不是在第一个语句中使用“Users”,我只想将它们放在一个语句中

我该怎么做?

2 个答案:

答案 0 :(得分:2)

如果您正在考虑组合两个查询中的行(即table1中的3行和来自table2的2行=使用最终查询的5行),您可以编写

SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2

要使UNION正常工作,两个查询中的字段数应与数据类型相同。

即。如果TABLE1有字段数字,数字,文本,日期 - TABLE2也应具有相同数量的字段(4)和数据类型的相同顺序(即数字,数字,文本,日期)。

编辑:我看过你修改过的问题。将User作为单独的查询,而不是使其成为MS-Access中一个完全不可读的查询。

保存时,Access不会保留SQL格式。因此,将查询拆分为不同的可重用部分比将所有内容都推送到自己的查询中是有意义的。

在您的示例中,如果您希望将Users作为主查询的一部分 - 则必须对两种情况(UNION的LEFT和RIGHT侧)重复这些操作。这没有意义。

编辑:这是一个大问题。感谢@Welbog。 在Access中保存查询时,格式将丢失。

EDIT2:看看这是否有帮助。我已将“USERS”查询包含在您的主sql中 我们的想法是匹配括号。

SELECT 
  users.id,
  users.first,
  users.last,
  chavrusas.luser_type AS user_type,
  chavrusas.id, 
  users.title, 
  users.city, 
  users.state, 
  users.home_phone, 
  users.email
FROM 
    (
        (
            (
                (tblusers AS Users
                 LEFT JOIN tblusershliach 
                 ON tblusers.id = tblusershliach.shliach_user_id
                )
                LEFT JOIN tbluserstudent 
                ON tblusers.id = tbluserstudent.student_user_id
            )
                LEFT JOIN tbluserstudentteacher
                ON tblusers.id = tbluserstudentteacher.student_teacher_user_id
        ) 
        LEFT JOIN tbluserteacher 
        ON tblusers.id = tbluserteacher.teacher_user_id;
    )
INNER JOIN chavrusas 
  ON Users.id=chavrusas.luser_id
WHERE     ((chavrusas.ruser_id)='3166' and chavrusas.ended=false) 
  AND     (chavrusas.luser_type) <> (chavrusas.ruser_type)
  AND NOT ((chavrusas.luser_type)='teacher' AND (chavrusas.ruser_type)='student')
  AND NOT ((chavrusas.ruser_type)='teacher' AND (chavrusas.luser_type)='student'); 
UNION
SELECT  
  users.id, 
  users.first, 
  users.last, 
  chavrusas.ruser_type AS user_type, 
  chavrusas.id, 
  users.title, 
  users.city, 
  users.state, 
  users.home_phone, 
  users.email
FROM
    (
        (
            (
                (tblusers AS Users
                 LEFT JOIN tblusershliach 
                 ON tblusers.id = tblusershliach.shliach_user_id
                )
                LEFT JOIN tbluserstudent 
                ON tblusers.id = tbluserstudent.student_user_id
            )
                LEFT JOIN tbluserstudentteacher
                ON tblusers.id = tbluserstudentteacher.student_teacher_user_id
        ) 
        LEFT JOIN tbluserteacher 
        ON tblusers.id = tbluserteacher.teacher_user_id;
    )
INNER JOIN chavrusas 
  ON Users.id=chavrusas.ruser_id
WHERE     ((chavrusas.luser_id)='3166' and chavrusas.ended=false)
  AND     (chavrusas.luser_type) <> (chavrusas.ruser_type)
  AND NOT ((chavrusas.luser_type)='teacher' AND (chavrusas.ruser_type)='student')
  AND NOT ((chavrusas.ruser_type)='teacher' AND (chavrusas.luser_type)='student')
ORDER BY 4;

答案 1 :(得分:1)

您的SQL语句在UNION关键字之前包含一个分号。我不确定Jet / ACE如何对待它,但我一直认为分号意味着“声明的结束”。丢弃它,看看你的结果是否有所不同。我完全不相信会解决你的问题,但我们要确保它没有贡献。

<强>更新: 我做了一些测试,看起来Jet / ACE只是忽略了UNION中的分号。我正在咆哮错误的树。