如果上述子查询的查询结果不是一条,就不能成功执行?[Err] 1242 - Subquery returns more than 1 row
2019-12-05
个人看法是链接的嵌套吧 或者子查询外面套上连接,并对连接进行更行。分析一下就是取经四人组和兄弟组内连接,得到孙悟空这个数据,并把它当作只有一条数据的数据表(个人觉得把这个表取个别名为C更好,更容易理解),再用A和这个数据表连接,对连接查询道德数据进行update操作。个人理解,欢迎指教!共勉!
2019-09-25
之前写错了, 这个才是我自己改的
-- 左外连接 取经四人组中不是悟空的兄弟的人
SELECT DISTINCT a.`id`,a.`user_name`
FROM user_a AS a LEFT OUTER JOIN user_b AS b ON a.`user_name`=a.`user_name`
WHERE a.`user_name` NOT IN (SELECT user_name FROM user_b);
执行结果是没错 意思也有的对上 但是总觉得有点繁琐。
-- 左外连接 取经四人组中不是悟空的兄弟的人
SELECT DISTINCT a.`id`,a.`user_name`
FROM user_a AS a LEFT OUTER JOIN user_b AS b ON a.`user_name`=a.`user_name`
WHERE a.`user_name` NOT IN (SELECT user_name FROM user_b);
执行结果是没错 意思也有的对上 但是总觉得有点繁琐。
2019-08-21
恕我直言一开始就应该给个完整表结构,对照表,写sql语句验证效果才会更容易理解。大家只是希望可以更加易懂,我没有说老师讲得不好,只是有的细节需要注意一下。
因为不知道完整的表结构,我直接按照老师给的两张表来建表,之后进行左外连接的测试 发现结果是空,我就自己按理解改了一下SQL语句
-- 左外连接 取经四人组中不是悟空的兄弟的人
SELECT DISTINCT a.`user_name`
FROM user_a AS a LEFT OUTER JOIN user_b AS b ON a.`user_name`=a.`user_name`
WHERE b.`user_name` IS NULL;
因为不知道完整的表结构,我直接按照老师给的两张表来建表,之后进行左外连接的测试 发现结果是空,我就自己按理解改了一下SQL语句
-- 左外连接 取经四人组中不是悟空的兄弟的人
SELECT DISTINCT a.`user_name`
FROM user_a AS a LEFT OUTER JOIN user_b AS b ON a.`user_name`=a.`user_name`
WHERE b.`user_name` IS NULL;
2019-08-21
2. 优化使用join 语句
select a.user_name , b.timestr , b.kills
from user1 a
join user_kills b on a.id = b.user_id
join user_kills c on c.user_id = b.user_id
group by a.user_name , b.timestr , b.kills
having b.kills = MAX(c.kills)
select a.user_name , b.timestr , b.kills
from user1 a
join user_kills b on a.id = b.user_id
join user_kills c on c.user_id = b.user_id
group by a.user_name , b.timestr , b.kills
having b.kills = MAX(c.kills)
2019-06-25
1. 使用子查询语句:
select a.user_name,b.timestr,b.kills from user1 as a left join user_kills as b on a.id = b.user_id
where b.kills = (select max(c.kills) from user_kills as c where b.user_id = c.user_id);
select a.user_name,b.timestr,b.kills from user1 as a left join user_kills as b on a.id = b.user_id
where b.kills = (select max(c.kills) from user_kills as c where b.user_id = c.user_id);
2019-06-25
UPDATE user1,user2 set user1.over = '齐天大圣' WHERE user1.`user_name` = user2.`user_name`; 这样也能操作
2019-05-03