Unfortunately, mysql does not have a built-in function that generates a series, as many other databases do). There are (at least) two ways to do this:
Rigidly set the required values as a subquery, then left join to the table:
select x.id, t.name from (select 1 id union select 2 union select 3 union select 4 union select 5) x left join my_table t on t.id = x.id
But it is tedious and difficult to code and maintain.
Or (as I did before) create a table (once) and fill in with natural numbers (once) to use as a proxy series generator:
create table numbers (num int); insert into numbers values (1), (2), (3), ... etc
then
select n.num id, t.name from numbers n left join my_table t on t.id = n.num where n.num in (1,2,3,4,5)
After setting up and filling in a large number of numbers, this approach is very convenient.
You can create a similar table filled with dates, used in a similar way, which is very convenient for creating numbers for each date in a range when not all dates have data.
Bohemian
source share