开发者

SQLite3 Database - SELECT query returning duplicates

I have been trying to build an SQL query to return matches between three tables.

SELECT DISTINCT thing.ip, thing.mac, 
    thing.vlan, thing.port 
FROM device 
INNER JOIN thing ON device.ip=thing.switch 
INNER JOIN portdetail ON device.ip=portdeail.poll_ip 
WHERE device.id="abc"

It originally worked perfectly until I added the second INNER JOIN, this resulted in no end of duplicate results. I then included the distinct keyword and it suddenly returned to normal again. As soon as I add in a field from the third table to the first select statement it goes back to showing many duplicates again. (portdetail.name).

I have found questions regarding this which have been solved by using the distinct keyword or by refining the ON selection. None of these appear to have helped (or at least they did until the addition of that last field)

SELECT DISTINCT thing.ip, thing.mac, 
    thing.vlan, thing.port, portdetail.name
FROM device 
INNER JOIN thing ON device.ip=thing.switch 
INNER JOIN portdetail ON device.ip=portdeail.poll_ip 
WHERE device.id="abc"

It is operating on an SQLite 3 database.

EDIT:

To clarify. device lists d开发者_运维问答evices such as switches. I then want to take the IP of a switch (device.ip and find some details on it from the thing table. The thing table being a table containing detail on the switches.

Now the portdetail table holds the name of the interfaces (Fa0/1, Fa0/2 etc) against the portdetail.poll_ip which is the IP of the switch, so I want to pull all interface names for a particular switch. I hope that is slightly clearer (I didn't name the database tables and have no power to change them at this stage)


Ok, so thanks to a clever bod in an IRC channel they suggested adding a second WHERE clause as at present I was only identifying from one table. Once I added a couple of extra WHERE clauses to identify fields between tables it all worked perfectly. (E.g. WHERE TABLE.FIELD == TABLE2.OTHERFIELD) Thanks for the suggestions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜