0

I'm trying to find a match for an IP address in any one of three tables but my query tells me the column I'm looking in is ambiguous?

SELECT COUNT(*) 
FROM `zz_tview`,`zz_tview1`,`zz_tview3` 
WHERE `ipaddress` ="192.168.01.01"

So I get this message "Column 'ipaddress' in where clause is ambiguous", how would I construct this firstly so it works and secondly so the query isn't too heavy as the tables have many thousands of rows?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Silo
  • 19
  • 5
  • You might be interested as well in the following Q&A: [Most efficient way to store IP Address in MySQL](http://stackoverflow.com/q/2542011/367456) – hakre Nov 05 '12 at 22:16

1 Answers1

2

Sounds like you want this, but this is going to produce a cartesian result which is most likely not the result that you want:

SELECT COUNT(*) 
FROM `zz_tview`,`zz_tview1`,`zz_tview3` 
WHERE `zz_tview`.`ipaddress` ="192.168.01.01"
  OR `zz_tview1`.`ipaddress` ="192.168.01.01"
  OR `zz_tview3`.`ipaddress` ="192.168.01.01"

You should really construct this as a JOIN:

SELECT COUNT(*) 
FROM `zz_tview` v
INNER JOIN `zz_tview1` v1
    ON v.id = v1.id  --- use the column that would join these values
INNER JOIN `zz_tview3` v3
    ON v.id = v3.id  --- use the column that would join these values
WHERE v.`ipaddress` ="192.168.01.01"
      OR v1.`ipaddress` ="192.168.01.01"
      OR v3.`ipaddress` ="192.168.01.01"

If you have no way to JOIN the tables, then you can use something similar to this:

select sum(total)
from
(
    SELECT count(*) as `total`
    FROM `zz_tview` v
    where v.`ipaddress` ="192.168.01.01"
    union all
    SELECT count(*) as `total`
    FROM `zz_tview1` v1
    where v1.`ipaddress` ="192.168.01.01"
    union all
    SELECT count(*) as `total`
    FROM `zz_tview3` v3
    where v3.`ipaddress` ="192.168.01.01"
) src
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • @Jeshurun I am aware that is why I added a second version of this with a warning that the initial version will produce a cartesian product. – Taryn Nov 05 '12 at 20:53
  • Yep, the second way is correct and you really should remove the first way as that will not return the correct count. – Jeshurun Nov 05 '12 at 20:55
  • 1
    He should leave it in, because shows the asker how to fix the query, then explains it is wrong, and shows the proper way. – hukir Nov 05 '12 at 20:57
  • I'm dead new to all this so it's teaching me something, i don't believe you should remove the wrong way, intially i thought of doing the OR but the query seemed to never end and i had to reboot mysql! – Silo Nov 05 '12 at 20:59
  • @Silo the problem with your initial version is you were creating a cartesian product of results which will produce a row for each combination, so the result can be a massive number of records to count – Taryn Nov 05 '12 at 21:01
  • @BF, you've commented "---use the column that would join these values" i don't really understand as all i was attempting to do was find if that ip address exists at least once in any of those three tables which all have the column "ipaddress" – Silo Nov 05 '12 at 21:04
  • @Silo if you are going to `JOIN` the tables, then you need to `JOIN` [(see a Visual Explanation of Joins)](http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html) ON a column. Since I cannot see your table structures I am guessing the column that would join them together – Taryn Nov 05 '12 at 21:06
  • @BF, thanks again, the only constant columns in all 3 tables are Uid and ipaddress, so what should i change? i only need to know that $ips (the variable that holds the ipaddress) appears in any of the tables or not. – Silo Nov 05 '12 at 21:09
  • @Silo if the `uid` is not consistent across the three tables for you to join on then I suggest using the last version I provided with the `UNION ALL` – Taryn Nov 05 '12 at 21:10
  • Great thanks :), i'll be back with another question on this because im actually using the query in a php file, but for now i'll mark the answer and raise another question - you've been brilliant! – Silo Nov 05 '12 at 21:14