Hello, there are two SQL queries:

SELECT s.date , s.group_id , s.duration, g.status_id, u.name FROM schedule s INNER JOIN user u ON s.user_id = u.id AND s.user_id = 13 INNER JOIN `group` g ON s.group_id = g.id AND g.status_id = 3 INNER JOIN group_history gh ON s.group_id = gh.group_id AND gh.status_id = 3 AND gh.end_date IS NULL WHERE s.date >= '2018-03-23 00:00:00' AND s.date <= gh.start_date; SELECT s.date , s.group_id , s.duration, g.status_id , u.name FROM schedule s INNER JOIN user u ON s.user_id = u.id AND s.user_id = 13 INNER JOIN `group` g ON s.group_id = g.id AND g.status_id = 2 WHERE s.date >= '2018-03-23 00:00:00' AND s.date <= '2018-03-31 23:59:59'; 

Table output:

enter image description here

enter image description here

Will it be possible to combine them into one table, because the fields are the same?

  • 2
    duck write UNION and try - teran
  • @teran when not fully learned SQL. Thank. : DD Write down the answer, offset. - Anatoliy.CHA
  • Read this.stackoverflow.com/questions/546912/… method 2 and 3 - nick_n_a
  • The union here is not the best (in speed) way, but ... the easiest. - nick_n_a

1 answer 1

The UNION operator is used to combine two or more result sets.

  • Each result set in UNION must have the same number of columns.
  • Columns must have similar data types.

The UNION operator selects unique values ​​by default . To resolve duplicate values, use UNION ALL .

Read more here .