如何解释 SQL 语句

来源:百度知道 编辑:UC知道 时间:2024/06/26 02:33:34
select aa.* from abc aa Where abc.g = 'china' and not exists
(select 1 from efg where hh= trim(aa.h) )
这条语句究竟要查什么东西?为什么使用“1”?
谢谢

select 后面用数字 表示要查询的 是第几列,select 1 表示 查询 表中的第1列;
用了谓词 exists 后面的 select 用* 就可以了,因为 exists 表示‘存在’的意思,not exists 表示 ‘不存在’;
整条语句表示的意思是 从adc这个表里面一条一条地取 满足 where 后面 g=‘china’的记录 来于 后面的 子查询比较。 若 子查询 select 1 from efg where hh= trim(aa.h) 不存在记录,则将 abc 表中的这条记录返回,若存在,则不显示在结果集中。

这个其实是个《子查询》以及《多表查询》
1.你的这个语句是有问题的
select aa.* from abc aa Where abc.g = 'china' and not exists
(select 1 from efg where hh= trim(aa.h) )
不信你可以测试以下
理想的写法应该是这样的
select aa.* from abc as aa Where aa.g in('china') and not exists
(select 1 from efg where hh = rtrim(ltrim(aa.h)))
2.这个语句查询的意思是
-- 在abc表中查询出aa.g字段为'china'
-- 并且abc表中的aa.h不存在于efg表的hh中
3.建议 多练习一下 多表(左右内)连接 以及 in like exists not exists等相关函数

1只是一个代号,其实用2,'a',什么都可以,not exists
(select 1 from efg where hh= trim(aa.h) )
代表select 1 from efg where hh= trim(aa.h)如果没有纪录就满足条件