To create a request for each COUNT (field_x), I think it is somehow not rational and cumbersome. I tried such a construction, give an example:

SELECT COUNT(`field_1`), COUNT(`field_2`), COUNT(`field_3`) FROM (SELECT `field_1`, `field_2`, `field_3`, `field_4` FROM `table_name` WHERE `date_field`="01.01.2017" AND (`field_1`="data_1" OR `field_1`="data_2" OR `field_1`="data_3")) sub WHERE `field_1`="data_1" AND `field_2`="data_2" 

It works, but the counter gives the amount from the last field field_2 = "data_2":

 COUNT(`field_1`) | COUNT(`field_2`) ----------------------------------- 1 | 1 

in fact should be (I would like to get such a table):

 COUNT(`field_1`) | COUNT(`field_2`) ----------------------------------- 3 | 1 

Is it possible to implement this in one request? I have 7 counters in the working code.

  • 2
    select sum(`field_1`="data_1"), sum(`field_2`="data_2") from ... - PetSerAl
  • @PetSerAl - thank you so much! - I_CaR

1 answer 1

You can subqueries. For example :

  SELECT COUNT(table1.columnName),(SELECT COUNT(table2.columnName) FROM table2 LIMIT 1), (SELECT COUNT(table3.columnName) FROM table3 LIMIT 1) FROM table1 

or you can even not use the column name as well COUNT(*)

  • Even at first glance, it somehow looks cumbersome. - I_CaR