How to combine two unrelated tables in mysql

There are two tables that are not related to each other (without foreign keys). How to show them together in MySQL?

TABLE1

enter image description here

table2

enter image description here

Result

enter image description here

+4
source share
5 answers

You can also use this:

SELECT t2.date, t1.name FROM table1 t1 CROSS JOIN table2 t2 
+6
source

Try it.

  SELECT t2.date, t1.name FROM table1 t1, table2 t2 ORDER BY t1.name ASC 
+2
source

Just try

 SELECT t2.date, t1.name FROM table1 t1, table2 t2 
0
source

Try the following: SELECT DATE, NAME FROM TABLE1, TABLE2

0
source

None of them will work.

If you want to know how to do it properly, I would suggest you take a look at this http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

CROSS JOIN not what you are looking for.

SQL will not be able to process this query. I suggest you make two sets of records with two different queries, and then sort them by field using PHP / Python / C or whatever your application is running on. Just do not leave this on the MySQL server because it cannot.

0
source

Source: https://habr.com/ru/post/1491256/


All Articles