搜档网
当前位置:搜档网 › 单表查询

单表查询

下面以一个例子说明如何使用SELECT从单个表中获取数据。
首先定义数据表fruits,输入语句如下:
+---------+--------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+---------+--------------+------+-----+---------+
| f_id | char(10) | NO | PRI | NULL |
| s_id | int(11) | NO | | NULL |
| f_name | char(50) | NO | | NULL |
| f_price | decimal(8,2) | NO | | NULL |
+---------+--------------+------+-----+---------

create table fruits(
f_id char(10) not null primary key,
s_id int(11) not null,
f_name char(50) not null,
f_price decimal(8,2) not null
);

为了演示如何使用SELECT语句,需要插入如下数据:
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| a2 | 103 | apricot | 2.20 |
| b2 | 104 | berry | 7.60 |
| b5 | 107 | xxxx | 3.60 |
| bj1 | 101 | blackberry | 10.20 |
| bs1 | 102 | orange | 11.20 |
| bs2 | 105 | melon | 8.20 |
| c0 | 101 | cherry | 3.20 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| m2 | 105 | xbabay | 2.60 |
| m3 | 105 | xxtt | 11.60 |
| o2 | 103 | coconut | 9.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
| t4 | 107 | xbababa | 3.60 |
+------+------+------------+---------+

insert into fruits
values('a1',101,'apple',5.2),
('a2',103,'apricot',2.2),
('b2',104,'berry',7.6),
('b5',107,'xxxx',3.6),
('bj1',101,'blackberry',10.2),
('bs1',102,'orange',11.2),
('bs2',105,'melon',8.2),
('c0',101,'cherry',3.2),
('l2',104,'lemon',6.4),
('m1',106,'mango',15.6),
('m2',105,'xbabay',2.6),
('m3',105,'xxtt',11.6),
('o2',103,'coconut',9.2),
('t1',102,'banana',10.3),
('t2',102,'grape',5.3),
('t4',107,'xbababa',3.6);


select 字段名 from 表名
where 条件1 and|or 条件2
group by 字段名
having 条件
order by 字段名 asc(升序)|desc(降序)
limit m,n;

limit m,n: 从第m+1行开始,返回n行数据
limit 5,10:从第6行开始,返回10行数据,也就是6-15行数据
limit 5;返回前5行数据


1、从fruits表中检索所有字段的数据,SQL语句如下:

select * from fruits;

2、查询fruits表中f_name列所有水果名称,SQL语句如下:

select f_name from fruits;

3、例如,从fruits表中获取f_name和f_price两列,SQL语句如下:

select f_name,f_price from fruits;

4、查询价格为10.2元的水果的名称,SQL语句如下:

select f_name,f_price from fruits
where f_price=10.2;

5、查找名称为“apple”的水果的价格,SQL语句如下:

select f_price from fruits
where f_name='apple';

6、查询价格小于10的水果的名称,SQL语句如下:

select f_name from fruits
where f_price<10

;

7、s_id为101和102的记录,SQL语句如下:

select * from fruits
where s_id=101 or s_id=102;

或者:
select * from fruits
where s_id in(101,102);


8、查询所有s_id不等于101也不等于102的记录,SQL语句如下:

select * from fruits
where s_id<>101 and s_id!=102;

或者:
select * from fruits
where s_id not in(101,102);

9、查询价格在2.00元到10.20元之间的水果名称和价格,SQL语句如下:

select f_name,f_price from fruits
where f_price between 2 and 10.2;

或者:
select f_name,f_price from fruits
where f_price f_price>=2 and f_price<=10.2;

10、查询价格在2.00元到10.20元之外的水果名称和价格,SQL语句如下:

select f_name,f_price from fruits
where f_price not between 2 and 10.2;

或者:
select f_name,f_price from fruits
where f_price f_price<2 or f_price>10.2;


UNION合并表的查询
union(去重的结果)
union all(不删除重复的结果)
11、查询数据表fruits中f_price小于10的结果和fruits_new中s_id为101和103的结果,SQL语句如下:

create table fruits_new as select * from fruits;

select * from fruits
where f_price<10
union
select * from fruits_new
where s_id in(101,103);


select * from fruits
where f_price<10
union all
select * from person;


去重:distinct
12、查询fruits表中的s_id有哪些:

select distinct s_id from fruits;

13、ORDER BY 字段名 asc(升序)|desc(降序):
查询数据表fruits中水果的信息,并按照价格进行升序排列,SQL语句为

select * from fruits
order by f_price desc;

14、查询数据表fruits中水果的信息,首先按照s_id升序,在按照f_price进行降序排列

select * from fruits
order by s_id asc,f_price desc;

LIMIT
15、查询数据表fruits中前5行的信息

select * from fruits
limit 5;

或者:
select * from fruits
limit 0,5;

16、查询数据表fruits中第3行到第6行的信息

select * from fruits
limit 2,4;

GROUP BY
17、查询数据表fruits中每个供应商各自水果的平均价格

分组是为了进行聚合运算

求最大值:select max(f_price) from fruits;
求最小值:select min(f_price) from fruits;
求和:select sum(f_price) from fruits;
求平均值:select avg(f_price) from fruits;
计数:select count(f_price) from fruits;

select s_id,avg(f_price)
from fruits
group by s_id;


HAVING
18、查询每个供应商各自水果的平均价格大于5元的记录;

select s_id,avg(f_price) as 平均价格
from fruits
group by s_id
having 平均价格>5;

19、查找f_name所有以’b’字母开头的水果,SQL语句如下:

通配符:% 任意多个字符
_ 任意单个字符

select * from fruits
where f_name like 'b%';


20、在fruits表中,查询f_name中包含字母'g’的记录,SQL语句如下:

select * from fruits
where f_name like '%g%';

21

、查询以’b’开头,并以’y’结尾的水果的名称,SQL语句如下:

select f_name from fruits
where f_name like 'b%y';

22、在fruits表中,查询以字母’y’结尾,且’y’前面只有4个字母的记录,SQL语句如下:

select f_name from fruits
where f_name like '____y';

23、在fruits表中,f_name中包含‘b’或者‘x'的记录,SQL语句如下:

select f_name from fruits
where f_name like '%b%' or f_name like '%x%';


相关主题