我正在尝试在三个表中的任何一个表中找到匹配的IP地址但我的查询告诉我我正在查找的列是不明确的?
SELECT COUNT(*)
FROM `zz_tview`,`zz_tview1`,`zz_tview3`
WHERE `ipaddress` ="192.168.01.01"
所以我得到这条消息“在'where子句中的列'ipaddress'是不明确的”,我将如何构造它以使其工作,其次所以查询不是太重,因为表有几千行?
答案 0 :(得分:2)
听起来你想要这个,但这会产生笛卡尔结果,这很可能不是你想要的结果:
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"
您应该将其构建为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"
如果您无法JOIN
表格,那么您可以使用类似的内容:
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