简体   繁体   English

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

[英]ms-access: select from another query

i am connecting access to a mysql db. 我正在连接对mysql数据库的访问。

i need to put together two statements and make them into one. 我需要将两个声明放在一起,然后将它们合并为一个。

for example: 例如:

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;

Users is a query which is: 用户是一个查询,它是:

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;

instead of using "Users" in the first statement, i just want to put them together into one statement 而不是在第一条语句中使用“用户”,我只想将它们放到一条语句中

how do i do it? 我该怎么做?

If you are looking at combining rows from both the queries (ie 3 rows from table1 & 2 rows from table2 = 5 rows using the final query), you could write 如果您正在考虑合并两个查询的行(即,使用最终查询,来自table1的3行和来自table2的2行= 5行),则可以编写

SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2

For UNION to work, the number of fields in both the queries should be same along with the data type. 为了使UNION正常工作,两个查询中的字段数以及数据类型应相同。

ie if the TABLE1 has fields numeric, numeric, text, date - TABLE2 should also have the same number of fields (4) and in same order of the data type (ie numeric, numeric, text, date). 即,如果TABLE1具有数字,数字,文本,日期字段-TABLE2还应该具有相同数量的字段(4),并且数据类型的顺序相同(即数字,数字,文本,日期)。

EDIT: I have looked at your modified question. 编辑:我已经看过您修改后的问题。 It is OK to have User as separate query than making it a one big totally unreadable query in MS-Access. 与将其作为MS-Access中一个完全不可读的大查询相比,可以将User作为单独的查询。

Access doesn't retain the SQL formatting when you save it. 保存时,Access不会保留SQL格式。 So, it makes sense to split the query into different re-usable pieces than to push everything into its own query. 因此,将查询分为不同的可重用部分比将所有内容推送到自己的查询中更有意义。

In your example, if you wish to have Users as part of the main query - it will have to be repeated for both case (LEFT and RIGHT side of UNION). 在您的示例中,如果希望将Users作为主查询的一部分-两种情况(UNION的LEFT和RIGHT端)都必须重复进行。 That doesn't make sense. 那没有道理。

EDIT: Here is the big query. 编辑:这是大查询。 Thanks to @Welbog. 感谢@Welbog。 The formatting will be lost when you save the query in Access. 当您将查询保存在Access中时,格式将丢失。

EDIT2: See if this helps. EDIT2:看看是否有帮助。 I have included the "USERS" query into your main sql. 我已经将“ USERS”查询包含到您的主sql中。
The idea is to match the brackets. 想法是匹配括号。

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;

Your SQL statement includes a semi-colon before the UNION keyword. 您的SQL语句在UNION关键字之前包含分号。 I'm not sure how Jet/ACE treats it, but I always thought the semi-colon meant "end of statement". 我不确定Jet / ACE如何处理它,但我一直认为分号的意思是“声明结束”。 Discard it and see if your results are any different. 丢弃它,看看结果是否有所不同。 I'm not at all confident that will fix your problem, but let's make sure it's not contributing. 我完全没有信心能解决您的问题,但请确保它没有帮助。

Update : I did some tests, and it looks like Jet/ACE just ignores a semi-colon within a UNION. 更新 :我做了一些测试,看起来Jet / ACE只是忽略了UNION中的分号。 I was barking up the wrong tree. 我叫错了树。

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

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