简体   繁体   English

为什么左联接不起作用而右联接起作用?

[英]Why left join doesn't work but right join works ?

i have 3 tables, 我有3张桌子

Transport.Devices, Transport.Conductors, Transport.ConductorDevices, Transport.Devices,Transport.Conductors,Transport.ConductorDevices,

i am applying LeftJoin on Devices table so it should also display those Devices which are not assigned, but it doesn't work , why ? 我在设备表上应用了LeftJoin,因此它还应该显示那些未分配的设备,但是它不起作用,为什么? i tried Right join and it worked but left doesn't work. 我尝试了“右连接”,它起作用了,但“左”不起作用。

Select Transport.ConductorDevices.ID, Transport.ConductorDevices.Device_ID,Transport.ConductorDevices.Conductor_ID,
        Transport.Conductors.Name as Conductor, Transport.Devices.TerminalSNO as Terminal
        from Transport.ConductorDevices
        Inner Join Transport.Devices
        ON Transport.Devices.DeviceID=Transport.ConductorDevices.Device_ID
        left Join Transport.Conductors
        ON Transport.Conductors.ConductorID= Transport.ConductorDevices.Conductor_ID

A left join != right join, a left join does include all on the left and the right join includes all on the right. 左连接!=右连接,左连接的确包含左侧的所有内容,而右连接则包含右侧的所有内容。 In this case you need all on the right. 在这种情况下,您需要所有权利。 That is why the right join is working for you. 这就是为什么正确的联接为您工作的原因。

sql_join_left sql_join_left

If you need all devices (not assigned too) you can start FROM with devices: 如果需要所有设备(也未分配),则可以从以下设备开始:

 from Transport.Devices
    LEFT JOIN Transport.ConductorDevices
    ON Transport.Devices.DeviceID=Transport.ConductorDevices.Device_ID
    LEFT JOIN Transport.Conductors
    ON Transport.Conductors.ConductorID= Transport.ConductorDevices.Conductor_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM