There is a table of products with the fields id_product , title , visibility , id_parent . The field with a non-unique value is id_parent . The task is to get the number of unique id_parent values ​​that satisfy the conditions. The construction below does not work; a clearly greater number of records are issued than the number of unique and satisfying conditions. I ask for advice in compiling the correct request.

  $query = "SELECT count(`id_product`) FROM `products` WHERE `title` LIKE '%".$title."%' AND `visibility` = '' group by `id_parent` "; 
  • one
    select count(distinct id_parent) ... and no group by, if you need one value - Mike

1 answer 1

 SELECT count(distinct id_parent) FROM `products` WHERE `title` LIKE '%".$title."%' AND `visibility` = '' 
  • Mike, is the query with distinct resource-intensive? The forums write that on a very large table can hang hosting. - 118_64
  • 2
    @ 118_64 Almost the same as group by. Sorted by this field and sorted is considered. Your like is the beginning % the same resource-intensive, guaranteed the entire table is moved, indexes cannot be used - Mike