Correct way to select from two tables in SQL Server with no common field to join on

后端 未结 3 1578
Happy的楠姐
Happy的楠姐 2020-12-25 11:04

Back in the old days, I used to write select statements like this:

SELECT 
table1.columnA, table2.columnA

FROM
table1, table2

WHERE
table1.columnA = \'Some         


        
3条回答
  •  悲&欢浪女
    2020-12-25 11:57

    Cross join will help to join multiple tables with no common fields.But be careful while joining as this join will give cartesian resultset of two tables. QUERY:

    SELECT 
       table1.columnA
     , table2,columnA
    FROM table1 
    CROSS JOIN table2
    

    Alternative way to join on some condition that is always true like

    SELECT 
       table1.columnA
     , table2,columnA
    FROM table1 
    INNER JOIN table2 ON 1=1
    

    But this type of query should be avoided for performance as well as coding standards.

提交回复
热议问题