假设我有一个评论数据库,我想看看有多少评论中有爱这个词,有多少评论中有恨这个词。很简单,对吧:
Select * from
(select count(*) from review where text like '%love%' ) as loves,
(select count(*) from review where text like '%hate%') as hates 问题是查询结果中的列名都是'count(*)',而我似乎不知道如何解决这个问题(我是SQL新手,所以这可能是微不足道的)。我正在使用sqlite。
发布于 2020-01-07 14:21:13
为此,您不需要子查询:
select sum(text like '%love%') as loves,
sum(text like '%hate%') as hates
from review SQLFiddle demo
发布于 2020-01-07 14:26:36
您的查询是正确的。但是,您可能希望对列使用别名,以便区分这两个计数。此外,逗号分隔的连接在1992年的标准SQL中变得多余。请改用显式连接,在本例中为CROSS JOIN。
select *
from (select count(*) as love_count from review where text like '%love%' ) as loves
cross join (select count(*) as hate_count from review where text like '%hate%') as hates;https://stackoverflow.com/questions/59623171
复制相似问题