A表(users)
字段:
uid int not null primary key,
uname nvarchar(30) not null
B表(results)
字段:
rid int not null primary key,
uid int not null(引用users表的uid)
查询A表中的数据没有出现在B表中
1.使用 not in
select * from users where uid not in (select uid from results)
2.使用 left join
select u.* from users as u left join results as r on u.uid = r.uid where r.uid is null
3.使用 not exists
select u.* from users as u where not exists(select * from results where uid = u.uid)
分享到:
相关推荐
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!
NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not in (select a from B) 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select ...
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!
比如说在LEFT JOIN查询里,一般来说左表就是主表,但这只是经验之谈,很多时候经验主义是靠不住的,为了说明问题,先来个例子,建两个演示用的表categories和posts: CREATE TABLE IF NOT EXISTS `categories` ( `...
查询数据是指从数据库中的数据表或视图中获取所需要的数据,在mysql中,可以使用SELECT语句来查询数据。根据查询条件的不同,数据库系统会找到不同的数据。 SELECT语句的基本语法格式如下: [sql] view plain copy ...
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right (outer) join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full/cross (outer...
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right (outer) join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full/cross (outer...
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right (outer) join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full/cross (outer...
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right outer join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full outer join: ...
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right (outer) join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full/cross (outer...
对于中小体量的项目而言,联表查询是再常见不过的操作了,尤其是在做报表的时候。然而校对数据的时候,您发现坑了吗?本篇文章就 mysql 常用联表查询复现常见的坑。 基础环境 建表语句 DROP TABLE IF EXISTS `role`...
8:用文本方式将数据装入数据库表中(例如D:/mysql.txt) mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt”INTO TABLE MYTABLE; 9:导入.sql文件命令(例如D:/mysql.sql) mysql>use database; mysql>source d:/...
有这样的一个问题mysql查询使用mysql中left(right)join筛选条件在on与where查询出的数据是否有差异。可能只看着两个关键字看不出任何的问题。那我们使用实际的例子来说到底有没有差异。例如存在两张表结构表结构1...
学习sql练习使用CREATE TABLE IF NOT EXISTS `area` ( `id` int(11) unsigned NOT NULL, ...select * from area a left join area b on a.id=b.pid left join area c on b.id=c.pid having a.pid=0 order by a.id;
SQL中表左连接(左外连接) left join SQL中表右连接(右外连接) right join SQL中表交叉连接(两张表的乘积) SQL中表全连接 full join SQL中变量 SQL中的事务 创建存储过程 存储过程带输出参数 调用存储过程 触发器定义...
8:用文本方式将数据装入数据库表中(例如D:/mysql.txt) mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE; 9:导入.sql文件命令(例如D:/mysql.sql) mysql>use database; mysql>source d:/mysql....