因为我想在这里提出更复杂的SQL SELECT语句,因为我很难在主题上找到正确的东西。
我有两张表具有完全相同的结构,如
TABLE A (id (INT(11)), time (VARCHAR(10));)
TABLE B (id (INT(11)), time (VARCHAR(10));)
现在我想要一个SELECT来计算两个表中特定id的编码。
SELECT COUNT(*) FROM TABLE A WHERE id = '1';
SELECT COUNT(*) FROM TABLE B WHERE id = '1';
所以我认为如果我使用一个SELECT而不是一个SELECT,那对数据库性能会好得多。
感谢您提供帮助
答案 0 :(得分:1)
SELECT COUNT(*) as count, 'tableA' as table_name FROM TABLEA WHERE id = '1'
union all
SELECT COUNT(*), 'tableB' FROM TABLEB WHERE id = '1'
答案 1 :(得分:1)
如果您想在单行中使用单独的计数,则可以使用子查询
SELECT
(SELECT COUNT(*) FROM TABLE A WHERE id = '1') a_count,
(SELECT COUNT(*) FROM TABLE B WHERE id = '1') b_count;
答案 2 :(得分:0)
你可以这样做:
select count(*)
from (
select id from t1 where id = 1
union all
select id from t2 where id = 1
) as t
另一种选择是:
select sum(cnt)
from (
select count(*) as cnt from t1 where id = 1
union all
select count(*) as cnt from t2 where id = 1
) as t