1

I have a query that returns records if found values in the IN clause. The matched records my vary from 0 to 1 and so thus the result of the query. But I want make the count as 5, even if rest rows are filled with NULL. Is there any way I can achieve this? As, for the given query

SELECT categoryID, categoryName
FROM category
WHERE categoryID
IN ( 52, 58, 60 , 62 , 64) 

I have values for the first 3 ids only and thus the result gives me 3 rows only. But I want the count of result to be 5 and the rest non-satisfying rows filled with null values to make the count. The current result:

CategoryID  CategoryName
52           ABC
58           XYZ
60           DEF

The required result

CategoryID  CategoryName
52           ABC
58           XYZ
60           DEF
NULL         NULL
NULL         NULL
6
  • What is reason for such logic? Commented Nov 27, 2014 at 9:36
  • I dont understand why you want to do that...? So if you will type IN ( 52, 58, 60 , 62 , 64, 85, 62, 82) its just returns more null vaules...? Commented Nov 27, 2014 at 9:37
  • While it's possible to do it directly in SQL, in most cases, it would be much easier to do it outside of SQL. @Nightmaresux's comment points out one other good question: what do you want to happen when the same ID is specified multiple times? Commented Nov 27, 2014 at 9:38
  • @hvd Is it really possible in only-SQL? Commented Nov 27, 2014 at 9:40
  • This might be achievable using code but I wanted to know if we could do so directly in plain SQL Commented Nov 27, 2014 at 9:41

2 Answers 2

3

Use a LEFT JOIN with a synthesized table that lists all the category IDs you want:

SELECT c.categoryID, c.categoryName
FROM (SELECT 52 AS categoryID
      UNION SELECT 58 UNION SELECT 60 UNION SELECT 62 UNION SELECT 64) AS a
LEFT JOIN category AS c ON a.categoryID = c.categoryID

Another way to do this is to create a table that contains all possible category IDs. Then use:

SELECT c.categoryID, c.categoryName
FROM allCategories AS a
LEFT JOIN category AS c ON a.categoryID = c.categoryID
WHERE a.categoryID IN (52, 58, 60 , 62 , 64)
Sign up to request clarification or add additional context in comments.

2 Comments

The OP wants categoryID to be NULL too if it's not found, so I think it should be c.categoryID rather than a.categoryID.
@hvd Good catch, that's a pretty strange requirement.
1

Add 5 null rows to the end and just select the first 5 rows.

SELECT categoryID, categoryName FROM (
SELECT categoryID, categoryName, 1 as ord 
FROM category
WHERE categoryID
IN ( 52, 58, 60 , 62 , 64)
UNION ALL
SELECT NULL as categoryID, NULL as categoryName, 0 as ord 
UNION ALL
SELECT NULL as categoryID, NULL as categoryName, 0 as ord 
UNION ALL
SELECT NULL as categoryID, NULL as categoryName, 0 as ord  
UNION ALL
SELECT NULL as categoryID, NULL as categoryName, 0 as ord  
UNION ALL
SELECT NULL as categoryID, NULL as categoryName, 0 as ord  )t 
ORDER BY ord desc LIMIT 0,5;

3 Comments

UNION removes duplicates, so this doesn't work. Creative answer though, and it can be made to work with UNION ALL.
This also handles the repeated ID case
@AakashJain You get the same results as with Barmar's first approach: neither will return the same category twice. Barmar's second approach might, depending on how you fill the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.