我有以下三个表
表a(id,name)作为主键
表b(id,name,country)作为主键,c_id作为外键
表c(id)作为主键,另一列是类型
我想从这些表中选择ID,名称和类型,其中country =' CA'
我的数据库是MySQL,我写的SQL如下:
select n.activity_date,n.advertiser_id,c.type as type
from a JOIN b as n
on (a.id=b.id,a.name=b.name)
JOIN c
on n.c_id=c.id
where b.country='CA'
limit 10;
错误是
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as n
on (a.activity_date=d.activity_date,a.advertiser_id=d.advertiser_id)
JOIN t' at line 2
答案 0 :(得分:2)
您必须将AND用于多个条件连接,而不是逗号。
select n.activity_date,n.advertiser_id,c.type as type
from a
JOIN b as n on a.id=n.id AND a.name=n.name
JOIN c on n.c_id=c.id
where n.country='CA'
limit 10;
答案 1 :(得分:1)
您的查询应该是
select n.activity_date,n.advertiser_id,c.type as type
from a JOIN b as n
-- because of the alias name you've got to reference table b as n
-- and you've omitted the AND operator
on (a.id=n.id AND a.name=n.name)
JOIN c
on n.c_id=c.id
where n.country='CA' -- you've got to use n instead of b
limit 10;
答案 2 :(得分:0)
您的on
不正确。您不要将这些子句与,
分开。你必须使用布尔逻辑,这意味着and
和or
,例如:
from a JOIN b as n
on (a.id=b.id AND a.name=b.name)
^^^^---- no commas here