mysql 1052 与 mysql 1242

今天在工作中,写sql语句时遇到了这两个sql错误。为记录,并不再重复出错,记此文。

简单说说背景。数据库中,有表 user(包括字段:user_id, username, useraccount)、log(包括字段:log_id,user_id, action, time, description)。需求是在这两张表中查询出username 和 time ,而且是通过传入的参数description字段获取的。有以下sql:

select t1.username, t2.time from user as t1, log t2 where t1.user_id = t2.user_id and user_id = (select user_id from log where description = "xxxx");

首先,mysql 报 1052 异常。经过查阅,是因为字段没有明确指明的原因,原来子查询的user_id没有明确指定,数据库不能够识别t1 还是t2 后作修改:

select t1.username, t2.time from user as t1, log t2 where t1.user_id = t2.user_id and t2.user_id = (select user_id from log where description = "xxxx");
此次也不能顺利查询到结果,并抛出了1242 错误:子查询中,返回的条目大于1 。此时发现,sql子查询用了“=”,将此该为 in 问题解决! 
select t1.username, t2.time from user as t1, log t2 where t1.user_id = t2.user_id and t2.user_id in (select user_id from log where description = "xxxx");



你可能感兴趣的:(项目经验)