我需要一个如下的结果
section_one | section_two
56788 678
这是我写的查询,分别有上面的结果
Select count(*) as section_one from tableNew
WHERE columnone LIKE %hjk%
以及
Select count(*) as section_two from tableNew
WHERE columnone NOT LIKE %hjk%;
如何将这两个组合在一个查询中并得到上述结果?
我试了下面的一些东西,但似乎没有正常工作
select *,
CASE
WHEN columnone LIKE %hjk% THEN count(1) as section_one
ELSE count(1) as section_two
END
from tableNew
Group by tableid;
在一个查询中同时使用这两个条件还有其他方法吗?
这方面的任何帮助都会很好的!
另外,还有没有其他方法来检查
boolean
的情况,比如不是columnone LIKE %hjk%
瞄准columnone IS NOT NULL
最佳答案
使用SUM
select
SUM(columnone LIKE %hjk%) as section_one,
SUM(columnone NOT LIKE %hjk%) as section_two
from tableNew
Group by tableid;
或者您可以使用
CASE
select
SUM(CASE WHEN columnone LIKE %hjk% THEN 1 ELSE 0 END) as section_one,
SUM(CASE WHEN columnone NOT LIKE %hjk% THEN 1 ELSE 0 END) as section_two
from tableNew
Group by tableid;