MySQL newbie question: Joining a list of strings to expand a table?

Viewed 35

bear with me, this is probably super easy but I just started SQL recently.

So I currently have a table that looks something like this:

SELECT startDate FROM table1
GROUP BY startDate
startDate
2020-01-01
2020-02-01
2020-03-01

But I'm trying to expand this into something like this, hopefully with a list of strings defined in the query itself:

startDate rank
2020-01-01 Rank 1
2020-01-01 Rank 2
2020-02-01 Rank 1
2020-02-01 Rank 2
2020-03-01 Rank 1
2020-03-01 Rank 2

From there I have some calculations in mind that I want to do. Is there any good way to actually do this?

1 Answers

Cross join the table with a synthesized table containing the ranks.

SELECT t1.startDate, t2.`rank`
FROM table1 AS t1
CROSS JOIN (
    SELECT 'Rank 1' AS `rank`
    UNION
    SELECT 'Rank 2'
) AS t2
Related