In order to retrieve rows from a table in a mysql database, where the column id is equal to 2, 4, 6, 8,….., can someone suggest alternative ways in which these values can be searched for without using IN. I guess we can use OR but is there ant other way?
Share
There are many ways. Some proposals:
Simply:
SELECT ... WHERE mod(id, 2) = 0In some SQL implementations you can use modulo operator
%instead ofmod().Conditional:
SELECT CASE mod(id, 2) WHEN 0 THEN ... ELSE ...Explanation of SELECT…CASE statement.
Good example.
Regular expression:
SELECT ... WHERE id LIKE '%[02468]'It returns records with only these id, which are ending with 0, 2, 4, 6 or 8. Even ones.