1.《SQL必知必会》--(更新中...)

时间:2019-09-01 21:47:25   收藏:0   阅读:116

第二课:检索数据

1.检索单个列

select prod_name from Products;

2.检索多个列

select prod_id,prod_name,prod_price from Products;

3.检索所有列

select * from Products;

4.检索不同的值(distinct:去重)

select distinct prod_name from Products;

5.限制结果(MySQL:limit)

select prod_name from Products
limit 5 offset 4;
select prod_name from Products
limit 4,5;

6.使用注释

select prod_id,prod_name,prod_price from Products; -- 这是一条注释

第三课 排序检索数据(order by语句)

1.排序数据(单个列)

select prod_name from Products
order by prod_name;

2.按多个列排序

select prod_id,prod_name,prod_price from Products
order by prod_price,prod_name;

3.按列位置排序

select prod_id,prod_name,prod_price from Products
order by 2,3;

4.指定排序方向

select prod_id,prod_name,prod_price from Products
order by prod_price,prod_name;
select prod_id,prod_name,prod_price from Products
order by prod_price desc,prod_name;
select prod_id,prod_name,prod_price from Products
order by prod_price desc,prod_name desc;

第四课 过滤数据(where语句)

1.使用where子句

select prod_id,prod_name,prod_price from Products
where prod_price = 3.49;

2.where子句操作符


=                                         (等于)
<>                                       (不等于)
!=                                        (不等于)
<                                         (小于)
<=                                       (小于等于)
!<                                        (不小于)
>                                         (大于)
>=                                       (大于等于)
!>                                        (不大于)
BETWEEN                         (在指定两个值之间)
IS NULL                              (为null值)

2.1 检查单个值

select prod_id,prod_name,prod_price from Products
where prod_price < 10;

2.2 不匹配检查

select prod_id,prod_name,prod_price from Products
where vend_id <> 'DLL01';

2.3 范围值检查

select prod_id,prod_name,prod_price from Products
where prod_price between 5 and 10;

2.4空值检查

select prod_id,prod_name,prod_price from Products
where prod_price IS NULL;

第五课 高级数据过滤(where 高阶操作)

1.组合where子句(and / or)

1.1 AND操作符(且,条件都要具备)

select prod_id,prod_name,prod_price from Products
where prod_price < 10 and prod_price > 5;

1.2 OR 操作符(或,条件其中某一个符合都可以)

select prod_id,prod_name,prod_price from Products
where prod_price = 10 or prod_price  = 5;

1.3 求值顺序

select prod_id,prod_name,prod_price from Products
where prod_price > 10 or prod_price < 5 and vend_id = 'DDL01';
select prod_id,prod_name,prod_price from Products
where (prod_price > 10 or prod_price < 5) 
        and vend_id = ' DDL01 ';

2.IN操作符(指定条件范围)

select prod_id,prod_name,prod_price from Products
where prod_price in (5,10,20);

3.NOT操作符

select prod_id,prod_name,prod_price from Products
where NOT vend_id  =  'DLL01';
select prod_id,prod_name,prod_price from Products
where vend_id <> 'DLL01';
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!