Select from Table1, Table2

后端 未结 3 763
盖世英雄少女心
盖世英雄少女心 2021-01-11 16:43

I found the following query and appreciate it if someone can help explain to me what this means.

select * from table1, table2
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-11 17:17

    This is called CROSS JOIN but using old syntax with , in FROM clause. My advice is not to use old syntax, stick with the JOIN here.

    It produces a cartesian product, so the number of rows in the result set will be the number of rows from table1 multiplied by number of rows from table2 (assuming there are no constraints in the WHERE clause). It effectively pairs each row from table1 with a row coming from table2.

    Below query is an equivalent but does explicit JOIN operation which separates constraint logic of data retrieval (normally put within the WHERE clause) from logic of connecting related data stored across separate tables (within the FROM clause):

    SELECT *
    FROM table1
    CROSS JOIN table2
    

    Consider an example where table1 has 8 rows and table2 has 5 rows. In the output, you get 40 rows (8 rows * 5 rows), because it pairs all rows from both sources (tables).

提交回复
热议问题