There is such a label Data .
| id | Key | Value | |----|-----|--------| | 1 | 0 | f30467 | | 2 | 1 | 406b67 | | 3 | 2 | c029bd | | 4 | 3 | 5c512f | | 5 | 4 | 739fd6 | | 6 | 5 | 096b71 | | 7 | 6 | b1e8b2 | | 8 | 7 | e71828 | | 9 | 8 | 8d8566 | | 10 | 9 | 94dcdc | Here is a sample of the required sample:
| Key | Value | |-----|--------| | 0 | f30467 | | 1 | 406b67 | | 2 | c029bd | | 2 | c029bd | | 0 | f30467 | I tried to use this query:
SELECT `Key`, `Value` FROM `Data` WHERE `Key` IN ('0', '1', '2', '2', '0') It does not give the desired result:
| Key | Value | |-----|--------| | 0 | f30467 | | 1 | 406b67 | | 2 | c029bd | Reference to an example in SQL Fiddle .
The following query gives the desired result, but this is not the most optimal solution for a large set of Key values:
SELECT `Key`, `Value` FROM `Data` WHERE `Key` = '0' UNION ALL SELECT `Key`, `Value` FROM `Data` WHERE `Key` = '1' UNION ALL SELECT `Key`, `Value` FROM `Data` WHERE `Key` = '2' UNION ALL SELECT `Key`, `Value` FROM `Data` WHERE `Key` = '2' UNION ALL SELECT `Key`, `Value` FROM `Data` WHERE `Key` = '0' Reference to an example in SQL Fiddle .
How to get the result I need without using the UNION ALL construction?