sql - A little help to better understand JOINS -


i have 3 tables substract data, , of data have query works pretty good, can't specific row , that's need help.

table 1:

equipmentid  | equipmentname | equipmenttypeid 15           | tesla         | 68 16           | colombus      | 93 

table 2:

equipmenttypeid | displayname      |  68              | electrical device| 93              | gps device       | 

table 3:

equipmentid  | ipaddress  | 15           | 192.168.1.1|   16           | 192.168.0.1| 

so far data following using folowing sql syntax:

select distinct t1.ipaddress, t2.equipmentid table3 t1 join table1 t2 on t1.equipmentid = t2.equipmentid ipaddress '%192%' 

the result looks like

ipaddress  | equipmentid | 192.168.1.1| 15          | 192.168.0.1| 16          | 

however when join followiing result messed up

select distinct t1.ipaddress, t2.equipmentid, t3.equipmenttypeid, t4.displayname table3 t1 join table1 t2 on t2.equipmentid = t1.equipmentid join table2 t3 on t3.equipmenttypeid = t1.equipmenttypeid join table2 t4 on t3.equipmenttypeid = t1.equipmenttypeid ipaddress '%192' 

but result following:

ipaddress  | equipmentid |equipmenttypeid| displayname     | 192.168.1.1| 15          |68             | electricaldevice| 192.168.1.1| 15          |93             | gps device      | 192.168.0.1| 16          |68             | electricaldevice| 192.168.0.1| 16          |93             | gps device      | 

any ideas on how right display name corresponding ipaddress , equipmentid?

if need more clarification please let me know. thank in advance

you joining table2 twice , on wrong id:

join table2 t3 on t3.equipmenttypeid = t1.equipmenttypeid join table2 t4 on t3.equipmenttypeid = t1.equipmenttypeid 

try way:

select distinct t3.ipaddress, t2.equipmentid, t3.equipmenttypeid, t2.displayname table3 t3 join table1 t1 on t1.equipmentid = t3.equipmentid join table2 t2 on t2.equipmenttypeid = t3.equipmenttypeid ipaddress '%192' 

note: changed names of joined tables because bit confusing.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -