-1

How do I left join other table but persist its root row?

Example

Table 1

ID Column 1 Column 2
1 Foo bar

Table 2

ID table_id Column 2
1 1 Foo bar
2 1 John Doe

Expected result

ID Column 1 table_id Column 2
1 foo null null (This row should persist because this is parent)
1 foo 1 Foo bar
1 foo 1 John Doe

Query

SELECT table1.id, table1.column1, table2.table_id, table2.column2
FROM table1
LEFT JOIN table2 ON table1.id = table2.table_id
Forbidden
  • 109
  • 2
  • 10
  • 1
    UNION will help. PS. You need not in LEFT joining - the output looks like CROSS JOIN. – Akina Apr 14 '23 at 05:02
  • Got solve it by, using UNION ALL and use it on select Got it here. https://stackoverflow.com/questions/3531251/using-union-and-order-by-clause-in-mysql – Forbidden Apr 14 '23 at 08:40

1 Answers1

-1

Got solve it by, using UNION ALL and use it on select

Got it here. Using union and order by clause in mysql

Forbidden
  • 109
  • 2
  • 10