我试图在三个表中的任何一个表中查找 IP 地址的匹配项,但我的查询告诉我我正在查看的列不明确?
SELECT COUNT(*)
FROM `zz_tview`,`zz_tview1`,`zz_tview3`
WHERE `ipaddress` ="192.168.01.01"
因此,我收到此消息“where 子句中的列 'ipaddress' 不明确”,我将如何首先构造它以使其工作,其次使查询不会太重,因为表有数千行?
听起来你想要这个,但这会产生一个笛卡尔结果,这很可能不是你想要的结果:
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