SQL单表查询语句

 查询某些字段不同记录:
            job字段中,可以发现有相同的数据,为了查询有多少种不同的job,在【命令编辑区】输入“select distinct job  from  scott.emp”   |   select distinct 字段名 from 数据表,这里的“distinct”保留字指在显示时去除相同的记录,与之对应的是“all”将保留相同的记录,默认为“all”。

表4.1                                                                        比较运算符
名称                                                                                                             实例
=(等于)                                                                 select  *  from  scott.emp  where  job=’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal=1100;  
!= (不等于)                                                           select  *  from  scott.emp  where  job!=’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal!=1100;
^=(不等于)                                                           select  *  from  scott.emp  where  job^=’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal^=1100;
<>(不等于)                                                           select  *  from  scott.emp  where  job<>’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal<>1100;
<(小于)                                                                 select  *  from  scott.emp  where  sal<2000;
                                                                              select  *  from  scott.emp  where  job<’MANAGER’;
>(大于)                                                                 select  *  from  scott.emp  where  sal>2000;
                                                                             select  *  from  scott.emp  where  job>’MANAGER’;
<=(小于等于)                                                      select  *  from  scott.emp  where  sal<=2000;
                                                                             select  *  from  scott.emp  where  job<=’MANAGER’;
>=(大于等于)                                                      select  *  from  scott.emp  where  sal>=2000;
                                                                             select  *  from  scott.emp  where  job>=’MANAGER’;
in(列表)                                                               select  *  from  scott.emp  where  sal in (2000,1000,3000);
                                                                            select  *  from  scott.emp  where  job in (’MANAGER’,’CLERK’);
not in(不在列表)                          select  *  from  scott.emp  where  sal not in (2000,1000,3000);
                                                      select  *  from  scott.emp  where  job not in (’MANAGER’,’CLERK’);
between(介于之间)                                          select  *  from  scott.emp  where  sal  between 2000 and 3000;
                                                     select  *  from  scott.emp  where  job  between  ’MANAGER’and CLERK’;
not between                                                      select  *  from  scott.emp  where  sal  not between 2000 and 3000;
(不介于之间)                               select  *  from  scott.emp  where  job  not between  ’MANAGER' and ’CLERK’;
like(模式匹配)                                                   select  *  from  scott.emp  where  job  like  ’M%’;
                                                                            select  *  from  scott.emp  where  job  like  ’M__’;
not like                                                               select  *  from  scott.emp  where  job  not  like  ’M%’;
(模式不匹配)                                                      select  *  from  scott.emp  where  job  not  like  ’M__’;
Is null                                                                 select  *  from  scott.emp  where  sal  is null;
(是否为空)                                                         select  *  from  scott.emp  where  job  is null;
is not null(是否为空)                                        select  *  from  scott.emp  where  sal  is  not  null;
                                                                            select  *  from  scott.emp  where  job  is  not  null;

SQL模糊查询,使用like比较字,加上SQL里的通配符,请参考以下:
      like和not like适合字符型字段的查询,%代表任意长度的字符串,_下划线代表一个任意的字符。like ‘m%’ 代表m开头的任意长度的字符串,like ‘m__’ 代表m开头的长度为3的字符串。      

1、LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。
2、LIKE'%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。
3、LIKE'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。
7、LIKE'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。
-------------------------------------------------
呵呵,要完整的例句啊。下面这句查询字符串是我以前写的,根据变量 zipcode_key 在邮政编码表 zipcode 中查询对应的数据,这句是判断变量 zipcode_key 为非数字时的查询语句,用 % 来匹配任意长度的字符串,从表中地址、市、省三列中查询包含关键字的所有数据项,并按省、市、地址排序。这个例子比较简单,只要你理解了方法就可以写出更复杂的查询语句。

sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address"

注:

"%"可以表示多个字符,
"_"只能表示一个字符,
一个汉字是两个字符,所以表示汉字应该用两个_,即“__”

 排序查询 :
在【命令编辑区】输入“select empno,ename,job from scott.emp where job<=’CLERK’ order by job asc,sal desc” |      order by 可以指定查询结果如何排序,形式为字段名 排序关键词;asc代表升序排列,desc代表降序排列,多个排序字段之间通过逗号分割。若有where查询条件,order by要放在where语句后面

分组查询 :
分组查询是指将查询结果按照字段分组。
(1)在【命令编辑区】输入“select  empno,ename,job,sal  from  scott.emp  group  by job,empno,ename,sal having sal<=2000”

(2)在【命令编辑区】输入“select  empno,ename,job,sal  from  scott.emp  where sal<=2000 group by job,empno,ename,sal”

where检查每条记录是否符合条件,having是检查分组后的各组是否满足条件。
having语句只能配合group by语句使用,没有group by时不能使用having,但可以使用
where。

变换查询显示
在【命令编辑区】输入“select  empno 编号,ename 姓名,job 工作,sal 薪水 from
scott.emp”,显示结果时将'empno'变成'编号' 等等.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值