0

I have a table like so

+---------+
| weekday |
+---------+
|    2    |
|    5    |
|    3    |
+---------+

Now I want to have a resultset in which I see the dates of this and the upcoming week like so:

+------------+---------+
|    date    | weekday |
+------------+---------+
| 2019-12-18 |    2    |
| 2019-12-21 |    5    |
| 2019-12-19 |    3    |
| 2019-12-25 |    2    |
| 2019-12-28 |    5    |
| 2019-12-26 |    3    |
+------------+---------+

So far I have this query

SELECT 
    CURDATE() + INTERVAL w.weekday - WEEKDAY(CURDATE()) DAY AS thisWeek,
    CURDATE() + INTERVAL w.weekday + 7 - WEEKDAY(CURDATE()) DAY AS nextWeek,
    dw.weekday
FROM
    weekdays AS w

Which gives me this result

+------------+------------+---------+
|  thisWeek  |  nextWeek  | weekday |
+------------+------------+---------+
| 2019-12-18 | 2019-12-25 |    2    |
| 2019-12-21 | 2019-12-28 |    5    |
| 2019-12-19 | 2019-12-26 |    3    |
+------------+------------+---------+

How would I have to need to proceed to get the former resultset?

1
  • Feel free to edit the question-titel as I didnt knew how to articulate it better.
    – InsOp
    Commented Dec 18, 2019 at 11:46

2 Answers 2

2

Use UNION ALL.

SELECT 
    CURDATE() + INTERVAL w.weekday - WEEKDAY(CURDATE()) DAY AS date
    dw.weekday
FROM
    weekdays AS w
UNION ALL
SELECT 
    CURDATE() + INTERVAL w.weekday + 7 - WEEKDAY(CURDATE()) DAY AS date
    dw.weekday
FROM
    weekdays AS w;
2
  • Would it be possible to achieve a similar result with a JOIN?
    – InsOp
    Commented Dec 19, 2019 at 8:04
  • Maybe with a full join and coalesce(). But a join isn't the ideal too here I guess.
    – sticky bit
    Commented Dec 19, 2019 at 15:04
1

You could try using UNION

select  thisWeek date, weekday
from  (

  SELECT 
      CURDATE() + INTERVAL w.weekday - WEEKDAY(CURDATE()) DAY AS thisWeek,
      CURDATE() + INTERVAL w.weekday + 7 - WEEKDAY(CURDATE()) DAY AS nextWeek,
      dw.weekday
  FROM   weekdays AS w

) t1 
union all  
select  nextWeek date, weekday 
from  (

  SELECT 
      CURDATE() + INTERVAL w.weekday - WEEKDAY(CURDATE()) DAY AS thisWeek,
      CURDATE() + INTERVAL w.weekday + 7 - WEEKDAY(CURDATE()) DAY AS nextWeek,
      dw.weekday
  FROM   weekdays AS w
) t2

or for avoid subquery you could create a view

create view my_view as  
SELECT 
      CURDATE() + INTERVAL w.weekday - WEEKDAY(CURDATE()) DAY AS thisWeek,
      CURDATE() + INTERVAL w.weekday + 7 - WEEKDAY(CURDATE()) DAY AS nextWeek,
      dw.weekday
  FROM   weekdays

then the query is

select  thisWeek date, weekday
from my_view 
union all  
select  nextWeek date, weekday 
from  my_view 
5
  • Would it be possible to achieve a similar result with a JOIN?
    – InsOp
    Commented Dec 19, 2019 at 8:04
  • Why a JOIN .. should be more expensive for performance .. in this case the UNION is the best way ..
    – ScaisEdge
    Commented Dec 19, 2019 at 16:00
  • because otherwise i have to write the subselect twice.
    – InsOp
    Commented Dec 19, 2019 at 16:58
  • You can create a view and use the view .. instead of the subquery
    – ScaisEdge
    Commented Dec 19, 2019 at 16:59
  • i would have to pass a parameter to it since both subqueries wouldnt be similiar, i.e. thisWeek and nextWeek would be date depending on the parameter
    – InsOp
    Commented Dec 19, 2019 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.