同一表上的比较选择(mysql)
我是mysql的新手,我想做一个简单的选择,但我想不出来 这就是我所拥有的:同一表上的比较选择(mysql),mysql,select,statements,Mysql,Select,Statements,我是mysql的新手,我想做一个简单的选择,但我想不出来 这就是我所拥有的: I got this in a Table named Control: | CODE | OFFICE | | 1 | usa | | 2 | usa | | 3 | usa | | 4 | usa | | 5 | usa | | 1 | china | | 3 | china | | 4 | china | And I need
I got this in a Table named Control:
| CODE | OFFICE |
| 1 | usa |
| 2 | usa |
| 3 | usa |
| 4 | usa |
| 5 | usa |
| 1 | china |
| 3 | china |
| 4 | china |
And I need get this:
| CODE | OFFICE |
| 2 | usa |
| 5 | usa |
然后,选择代码,office,其中代码尚未在office=china注册
我必须做一个自连接或类似的东西,或者使用GROUP BY语句?
我被困在这里。。。我真的非常感谢您的帮助。我想这很有效
create table Test(id integer, code integer, office varchar(100));
insert into Test(id, code, office) values(1, 1, "usa"),(2, 2, "usa"),(3, 3, "usa"),(4, 4, "usa"),(5, 5, "usa"),(6, 1, "china"),(7, 3, "china"),(8, 4, "china");
select * from Test;
Select * from Test where code NOT IN (select code from Test where office = "china");
您必须使用子查询 > P>你可以在<代码>代码<代码>上做一个“自左连接”,只考虑右边没有匹配的行,即右侧值<代码>为null
SELECT
tleft.*
FROM Control AS tleft
LEFT JOIN Control AS tright
ON tright.Code = tleft.Code AND
tright.Office = 'china'
WHERE tleft.Office = 'usa' AND
tright.Code IS NULL -- this filters no matching code found in china