Home mysql Counting the number of unique values ​​with multiple conditions

Counting the number of unique values ​​with multiple conditions

Author

Date

Category

There is a table products with fields id_product , title , visibility , id_parent … The field with a non-unique value is id_parent . The goal is to get the number of unique id_parent values ​​that match the conditions. The construction below does not work, there are clearly more records returned than the number of unique and satisfying conditions. I ask for advice in drawing up the correct request.

$ query = "SELECT count (` id_product`) FROM `products`
      WHERE `title` LIKE '%". $ Title. "%'
      AND `visibility` = ''
      group by `id_parent`
     ";

Answer 1

SELECT count (distinct id_parent)
 FROM `products`
 WHERE `title` LIKE '%". $ Title. "%'
  AND `visibility` = ''

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions