SELECT without FROM Clause

馋奶兔 提交于 2021-01-28 01:08:49

问题


I'm writing a SQL statement for my MS Access database, and the purpose is to count values from 3 different queries, so I tried this way:

SELECT(query1 + query2 + query3) AS Qtd

Each query returns an unique value from an aggregate function count, i.e, query1 = SELECT Count(something) FROM Table WHERE...

Everything should work fine, but MS Access demands a FROM clause. When I put a table in that query (not changing the SELECT statement above), I end up with tones of rows and each row the result expected from Qtd column.

So is there any way to skip the FROM Clause or the only option to work around is write TOP 1 (or DISTINCT) to not get tones of duplicated rows because of the unnecessary table in FROM clause?


回答1:


Consider the cross join (comma separated tables) of the aggregate queries:

SELECT (query1.CntColumn + query2.CntColumn + query3.CntColumn) AS Qtd
FROM query1, query2, query3



回答2:


You could union all the queries and then sum all the results:

SELECT SUM(cnt)
FROM   (SELECT COUNT(*) AS cnt FROM table1 WHERE ...
        UNION ALL
        SELECT COUNT(*) AS cnt FROM table2 WHERE ...
        -- Etc..
       ) t


来源:https://stackoverflow.com/questions/49201428/select-without-from-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!