此SQL命令中的错误
select account_id,fname,lname ,
(select email from account where id=account_id) as email
from user_account
where account_id in (
select destination_id
from friendship
where source_id=$user_id)
and (concat(fname,lname) like '%a%' or email like '%a%')
email
是account
表
错误消息为:Unknown column email
我想收到电子邮件并将其与fname + lname
一样进行测试答案 0 :(得分:2)
听起来您的表user_account
没有在WHERE子句中引用的名为email
的列。请尝试使用JOIN
代替:
select ua.account_id,
ua.fname,
ua.lname,
a.email
from user_account ua
inner join account a where ua.id = a.account_id
and a.account_id in ( select destination_id from friendship where source_id=$user_id)
and (concat(fname,lname) like '%a%' or email like '%a%');
答案 1 :(得分:1)
重新格式化查询的易读性:
SELECT account_id, fname, lname,
(SELECT email FROM account WHERE id = account_id) AS email
FROM user_account
WHERE account_id IN (SELECT destination_id FROM friendship WHERE source_id = $user_id)
AND (CONCAT(fname, lname) LIKE '%a%' OR email LIKE '%a%')
最可能的原因是列email
在主WHERE子句中可以引用的唯一表是user_account
,但email
列只能在account
表。
最简单的解决方法是删除选择列表中的子查询并改为使用常规连接:
SELECT u.account_id, u.fname, u.lname, a.email
FROM user_account AS u
JOIN account AS a ON a.id = u.account_id
WHERE account_id IN (SELECT destination_id
FROM friendship AS f
WHERE f.source_id = $user_id)
AND (CONCAT(u.fname, u.lname) LIKE '%a%' OR a.email LIKE '%a%');
我假设$user_id
是一个输入值,并且符号在您的系统中有意义;它通常在SQL中无效。
目前尚不清楚IN条款有多大好处;也可以与其他表格建立联接。
答案 2 :(得分:0)
我假设您在Accout表中的ID与User_Account中的Account_id以及友谊表中的目标ID相同。
select ua.account_id,
ua.fname,
ua.lname,
a.email
from user_account ua
inner join account a where ua.id = a.account_id
and a.account_id
in ( select destination_id from friendship where source_id=$user_id) and (concat(fname,lname) like '%a%' or email like '%a%')
答案 3 :(得分:0)
此SQL命令中的错误
select account_id,fname,lname ,
(select email from account where id=account_id) as email from user_account
where account_id
in ( select destination_id from friendship where source_id=$user_id) and (concat(fname,lname) like '%a%' or email like '%a%')
问题在于,在以下语法中,电子邮件是表别名,而不是列名
(select email from account where id=account_id) as email
看起来你已经从p.campell获得了更好的方法