`
pengleong
  • 浏览: 5858 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类

三种方式查询A表的数据没有出现在B表的数据中(not in, left join, not exists)

SQL 
阅读更多
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)
分享到:
评论
1 楼 xiangch 2011-04-21  
引用
uid int not null(引用users表的uid)

引用
1.select u.* from users as u left join results as r on u.uid = r.uid where r.uid is null

不矛盾吗?

相关推荐

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!

    mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

    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

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!

    MySQL的LEFT JOIN表连接的进阶学习教程

    比如说在LEFT JOIN查询里,一般来说左表就是主表,但这只是经验之谈,很多时候经验主义是靠不住的,为了说明问题,先来个例子,建两个演示用的表categories和posts: CREATE TABLE IF NOT EXISTS `categories` ( `...

    MySql基本查询、连接查询、子查询、正则表达查询讲解

    查询数据是指从数据库中的数据表或视图中获取所需要的数据,在mysql中,可以使用SELECT语句来查询数据。根据查询条件的不同,数据库系统会找到不同的数据。 SELECT语句的基本语法格式如下: [sql] view plain copy ...

    数据库操作语句大全(sql)

    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语句大全

    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经典语句一部分

    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语句大全

    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语句

    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联表查询基本操作之left-join常见的坑

    对于中小体量的项目而言,联表查询是再常见不过的操作了,尤其是在做报表的时候。然而校对数据的时候,您发现坑了吗?本篇文章就 mysql 常用联表查询复现常见的坑。 基础环境 建表语句 DROP TABLE IF EXISTS `role`...

    MySQL命令大全

    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 left( right ) join使用on与where筛选的差异

    有这样的一个问题mysql查询使用mysql中left(right)join筛选条件在on与where查询出的数据是否有差异。可能只看着两个关键字看不出任何的问题。那我们使用实际的例子来说到底有没有差异。例如存在两张表结构表结构1...

    area.sql.txt

    学习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;

    数据库和ado知识

    SQL中表左连接(左外连接) left join SQL中表右连接(右外连接) right join SQL中表交叉连接(两张表的乘积) SQL中表全连接 full join SQL中变量 SQL中的事务 创建存储过程 存储过程带输出参数 调用存储过程 触发器定义...

    MYSQL常用命令大全

    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....

Global site tag (gtag.js) - Google Analytics