Pankaj Khurana Pankaj Khurana - 8 days ago 7
MySQL Question

Need Help Regarding MySQL IN Clause

I have table named collections in which there are fields pk_collectionid and name.

I am generating a string containing the collection ids separated by commas. Format is :

1,1


I want to retrieve the name of all the ids mentioned in the string from collections table.

I have used
IN clause under mysql
.

My query is:

SELECT name
FROM `collections`
WHERE `pk_collectionid`
IN ( 1, 1 )


Right now it is generating one row as id is common but i want that it should show two rows instead of one even if ids are same.

Answer

This is a very strange requirement. The only solution I could think of is to use a UNION ALL query:

SELECT name FROM `collections` WHERE `pk_collectionid` = 1
UNION ALL
SELECT name FROM `collections` WHERE `pk_collectionid` = 1
-- UNION ALL
-- SELECT name FROM `collections` WHERE `pk_collectionid` = 2
-- UNION ALL
-- SELECT name FROM `collections` WHERE `pk_collectionid` = 3
-- ...
Comments