SQL Select rows using LEFT JOIN - where count is 0 matches
|I need to get rows from one SQL table when there are zero matches for a specific condition in the second table. I am using SQL SELECT with LEFT JOIN. How can I create a condition with GROUP BY and COUNT to get related rows with 0 matches for a specific value ?|
To get only rows from one table with zero matches for a specific condition in the second table, you can use a subquery with SELECT and COUNT(), without using of LEFT JOIN.
For example, the following solution will get categories with zero related items:
SELECT c.category_name FROM item_categories AS c WHERE 'zero_matches' = (SELECT IF(COUNT(*) = 0, 'zero_matches', 'matches_found') FROM items AS i WHERE c.id_category = i.id_category) ORDER BY c.category_name;
You can also add GROUP BY into SQL if needed.
|How to calculate inverse currency exchange rate ?|
|How to add side by side rows in excel pivot table ?|
|Remove or hide bottom navigation bar with buttons on Android|
|How to change QWERTZ keyboard to QWERTY in Windows 10 ?|
|Incorrect integer value '' for column 'user_id' at row 1 - error|
|How to calculate how many Man Days are 3 Man Hours ?|
|Check if value exists in column more than once or twice - Excel|