1.How to count the number of different values of the same column in the same query, so as to reduce the number of queries.
For example, suppose you need to return the number of list menus at different levels through a query, you can query by the following statement:
select SUM(IF(functionlevel=’0′,1,0)) as Level 1, SUM (IF (functionlevel=’1′, 1,0)) as second,
SUM(IF(functionlevel=’2′,1,0)) as Level third, SUM (IF (functionlevel=’3′, 1,0)) as fourth, SUM (IF (functionlevel=’4′, 1,0)) as fifth.
, SUM(IF(functionlevel=’5′,1,0)) as Sixth level from t_s_function
Or:
select SUM(functionlevel=’0′) as Level 1, SUM (functionlevel=’1′) as second,
SUM(functionlevel=’2′) as Level third, SUM (functionlevel=’3′) as fourth, SUM (functionlevel=’4′) as fifth.
, SUM(functionlevel=’5′) as Sixth level from t_s_function
You can also use the following statements to query:
select COUNT(functionlevel=’0′ or null) as Level 1, COUNT (functionlevel=’1’or null) as second,
COUNT(functionlevel=’2′ or null) as Level 3, COUNT (function level =’3’or null) as Level 4, COUNT (function level =’4′ or null) as Level 5
, COUNT(functionlevel=’5′ or null) as Sixth level from t_s_function
Deformation:
select count(*),functionlevel from t_s_function GROUP BY functionlevel
2.