我按以下方式进行查询:
SELECT * FROM frequency WHERE docid = '10398_txt_earn' AND count = 1
UNION
SELECT * FROM frequency WHERE docid = '925_txt_trade' AND count = 1;
输出正确
docid term count
10398_txt_earn 100 1
10398_txt_earn 11340 1
10398_txt_earn 12 1
10398_txt_earn 1204 1
10398_txt_earn 198485 1
10398_txt_earn 20 1
10398_txt_earn 27 1
10398_txt_earn 28 1
现在我想用COUNT()运算符计算结果中的行数,但我不知道应用它。提前致谢
Ubuntu14.04 SQLite with XAMPP
答案 0 :(得分:1)
你在找这个吗?
select count(*)
FROM (SELECT * FROM frequency WHERE docid = '10398_txt_earn' AND count = 1
UNION
SELECT * FROM frequency WHERE docid = '925_txt_trade' AND count = 1
) t;
顺便说一句,您可以更有效地编写原始查询:
SELECT *
FROM frequency
WHERE count = 1 and (docid in '10398_txt_earn', '925_txt_trade');
然后计数:
SELECT count(*)
FROM frequency
WHERE count = 1 and (docid in '10398_txt_earn', '925_txt_trade');
答案 1 :(得分:1)
你可以这样做:
SELECT COUNT(1)
FROM
(
SELECT * FROM frequency WHERE docid = '10398_txt_earn' AND count = 1
UNION
SELECT * FROM frequency WHERE docid = '925_txt_trade' AND count = 1
) a
由于您使用了' UNION',您的结果将删除重复项。如果您想要包含所有记录(包括任何重复记录),请使用UNION ALL
代替UNION
。