SQL一个查询中的多个select count语句

时间:2021-04-10 00:23:33

I have data with no relation. Just need to count various columns from 3 tables and display them on the page as a view.

我有没有关系的数据。只需要计算3个表中的各个列,并将它们作为视图显示在页面上。

This is the code so far but doesn't work:

这是到目前为止的代码,但不起作用:

SELECT COUNT(cars) AS A,
       (SELECT COUNT(boats) FROM tableBoats) AS B,
       (SELECT COUNT(trees) FROM tableTrees) AS C,
 FROM tableCars

5 个解决方案

#1


12  

SELECT A, B, C
FROM (SELECT COUNT(cars) as A FROM tableCars) a
CROSS JOIN (SELECT COUNT(boats) as B FROM tableBoats) b
CROSS JOIN (SELECT COUNT(trees) as C FROM tableTrees) c

should do it.

应该这样做。

#2


13  

Assuming you have a table like here (tableXxx tables having a field named xxx), your query had a syntax error by having a comma after AS C,, without that comma, it works properly (at least using sqlite, because mssql is not working at sqlfiddle for me):

假设你有一个像这里的表(tableXxx表有一个名为xxx的字段),你的查询有一个语法错误,在AS C后面有一个逗号,没有逗号,它工作正常(至少使用sqlite,因为mssql不工作at sqlfiddle对我来说):

http://sqlfiddle.com/#!5/5fa6c/3

http://sqlfiddle.com/#!5/5fa6c/3

SELECT COUNT(cars) AS A,
       (SELECT COUNT(boats) FROM tableBoats) AS B,
       (SELECT COUNT(trees) FROM tableTrees) AS C
FROM tableCars

BTW, you could simplify your query to

顺便说一下,你可以简化你的查询

SELECT (SELECT COUNT(cars ) FROM tableCars ) AS A,
       (SELECT COUNT(boats) FROM tableBoats) AS B,
       (SELECT COUNT(trees) FROM tableTrees) AS C

The other answers are also perfect :)

其他答案也很完美:)

#3


3  

How about this?

这个怎么样?

SELECT
    (SELECT COUNT(*) FROM tableCars) car_count,
    (SELECT COUNT(*) FROM tableBoats) boat_count,
    (SELECT COUNT(*) FROM tableTrees) tree_count

#4


1  

Following on Luc M's response, which provides a single list, vs columns with separate values. You can imagine how useful this might be...

继Luc M的响应(提供单个列表)与具有单独值的列之后。你可以想象这可能是多么有用......

SELECT C.Accountnum as AccountNum, C.Address as Address, 'C' as Source 
From CustTable C 
Where C.AccountNum like '000%' 
Union All 
Select V.Accountnum as AccountNum, V.Name as Address, 'V' as Source 
from VendTable V 
Where V.AccountNum like 'A%'

#5


0  

SELECT *
FROM 
(
   SELECT 'Nb cars'  as description, COUNT(cars)  AS count_item FROM tableCars
   UNION ALL
   SELECT 'Nb boats' as description, COUNT(boats) AS count_item FROM tableBoats
   UNION ALL
   SELECT 'Nb tress' as description, COUNT(trees) AS count_item FROM tableTrees
) temp

#1


12  

SELECT A, B, C
FROM (SELECT COUNT(cars) as A FROM tableCars) a
CROSS JOIN (SELECT COUNT(boats) as B FROM tableBoats) b
CROSS JOIN (SELECT COUNT(trees) as C FROM tableTrees) c

should do it.

应该这样做。

#2


13  

Assuming you have a table like here (tableXxx tables having a field named xxx), your query had a syntax error by having a comma after AS C,, without that comma, it works properly (at least using sqlite, because mssql is not working at sqlfiddle for me):

假设你有一个像这里的表(tableXxx表有一个名为xxx的字段),你的查询有一个语法错误,在AS C后面有一个逗号,没有逗号,它工作正常(至少使用sqlite,因为mssql不工作at sqlfiddle对我来说):

http://sqlfiddle.com/#!5/5fa6c/3

http://sqlfiddle.com/#!5/5fa6c/3

SELECT COUNT(cars) AS A,
       (SELECT COUNT(boats) FROM tableBoats) AS B,
       (SELECT COUNT(trees) FROM tableTrees) AS C
FROM tableCars

BTW, you could simplify your query to

顺便说一下,你可以简化你的查询

SELECT (SELECT COUNT(cars ) FROM tableCars ) AS A,
       (SELECT COUNT(boats) FROM tableBoats) AS B,
       (SELECT COUNT(trees) FROM tableTrees) AS C

The other answers are also perfect :)

其他答案也很完美:)

#3


3  

How about this?

这个怎么样?

SELECT
    (SELECT COUNT(*) FROM tableCars) car_count,
    (SELECT COUNT(*) FROM tableBoats) boat_count,
    (SELECT COUNT(*) FROM tableTrees) tree_count

#4


1  

Following on Luc M's response, which provides a single list, vs columns with separate values. You can imagine how useful this might be...

继Luc M的响应(提供单个列表)与具有单独值的列之后。你可以想象这可能是多么有用......

SELECT C.Accountnum as AccountNum, C.Address as Address, 'C' as Source 
From CustTable C 
Where C.AccountNum like '000%' 
Union All 
Select V.Accountnum as AccountNum, V.Name as Address, 'V' as Source 
from VendTable V 
Where V.AccountNum like 'A%'

#5


0  

SELECT *
FROM 
(
   SELECT 'Nb cars'  as description, COUNT(cars)  AS count_item FROM tableCars
   UNION ALL
   SELECT 'Nb boats' as description, COUNT(boats) AS count_item FROM tableBoats
   UNION ALL
   SELECT 'Nb tress' as description, COUNT(trees) AS count_item FROM tableTrees
) temp