欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL单表查询常见操作实例总结

程序员文章站 2022-07-05 17:17:21
本文实例总结了mysql单表查询常见操作。分享给大家供大家参考,具体如下: 创建fruits表: create table fruits ( f_id...

本文实例总结了mysql单表查询常见操作。分享给大家供大家参考,具体如下:

创建fruits表:

create table fruits
(
 f_id  char(10)   not null,
 s_id  int not null,
 f_name char(255)   not null,
 f_price decimal(8,2) not null,
 primary key(f_id)
) ;
insert into fruits (f_id, s_id, f_name, f_price)
values('a1', 101,'apple',5.2),
('b1',101,'blackberry', 10.2),
('bs1',102,'orange', 11.2),
('bs2',105,'melon',8.2),
('t1',102,'banana', 10.3),
('t2',102,'grape', 5.3),
('o2',103,'coconut', 9.2),
('c0',101,'cherry', 3.2),
('a2',103, 'apricot',2.2),
('l2',104,'lemon', 6.4),
('b2',104,'berry', 7.6),
('m1',106,'mango', 15.6),
('m2',105,'xbabay', 2.6),
('t4',107,'xbababa', 3.6),
('m3',105,'xxtt', 11.6),
('b5',107,'xxxx', 3.6);

常用查询:

select * from fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";

创建customers表:

create table customers
(
 c_id   int    not null auto_increment,
 c_name  char(50) not null,
 c_address char(50) null,
 c_city  char(50) null,
 c_zip   char(10) null,
 c_contact char(50) null,
 c_email  char(255) null,
 primary key (c_id)
);
insert into customers(c_id, c_name, c_address, c_city, c_zip, c_contact, c_email)
values(10001, 'redhook', '200 street ', 'tianjin', '300000', 'liming', 'lming@163.com'),
(10002, 'stars', '333 fromage lane', 'dalian', '116000', 'zhangbo','jerry@hotmail.com'),
(10003, 'netbhood', '1 sunny place', 'qingdao', '266000', 'luocong', null),
(10004, 'joto', '829 riverside drive', 'haikou', '570000', 'yangshan', 'sam@hotmail.com');

常用查询语句:

select c_id,c_name,c_email from customers where c_email is null;
select c_id,c_name,c_email from customers where c_email is not null;
select f_name,f_price from fruits where s_id=101 and f_price>=5;
select f_name,f_price from fruits where s_id in (101,102) and f_price >=5 and f_name="apple";
select f_name,f_price from fruits where s_id=101 or s_id=102;

使用in操作更加简洁明了

select f_name,f_price
from fruits
where s_id in (101 ,102);

字段不重复

select distinct s_id from fruits;
select f_name from fruits order by f_name;

如果第一列数据中没有相同值,将不再对第二列进行排序。

select f_name, f_price
from fruits
order by f_name, f_price;

按价格降序排列,desc为降序,默认为升序。

select f_name, f_price from fruits order by f_price desc;
select f_name, f_price from fruits order by f_price desc,f_name;
select s_id, count(*) as total from fruits group by s_id;
select s_id, group_concat(f_name) as names from fruits group by s_id;

使用having过滤分组

select s_id, group_concat(f_name) as names
from fruits
group by s_id having count(f_name)>1;

group by 子句中使用with rollup

select s_id, count(*) as total from fruits group by s_id with rollup;
select * from fruits group by s_id,f_name;

创建orderitems表:

create table orderitems
(
 o_num   int     not null,
 o_item   int     not null,
 f_id    char(10)   not null,
 quantity  int     not null,
 item_price decimal(8,2) not null,
 primary key (o_num,o_item)
) ;
insert into orderitems(o_num, o_item, f_id, quantity, item_price)
values(30001, 1, 'a1', 10, 5.2),
(30001, 2, 'b2', 3, 7.6),
(30001, 3, 'bs1', 5, 11.2),
(30001, 4, 'bs2', 15, 9.2),
(30002, 1, 'b3', 2, 20.0),
(30003, 1, 'c0', 100, 10),
(30004, 1, 'o2', 50, 2.50),
(30005, 1, 'c0', 5, 10),
(30005, 2, 'b1', 10, 8.99),
(30005, 3, 'a2', 10, 2.2),
(30005, 4, 'm1', 5, 14.99);

常用查询语句:

select o_num, sum(quantity * item_price) as ordertotal
from orderitems
group by o_num
having sum(quantity*item_price) >= 100;
select o_num, sum(quantity * item_price) as ordertotal
from orderitems
group by o_num
having sum(quantity*item_price) >= 100
order by ordertotal;
select * from fruits limit 8;

从第五行开始,读取3行

select * from fruits limit 4,3;

【例.1】从fruits表中检索所有字段的数据

select * from fruits;
select f_id, s_id ,f_name, f_price from fruits;

【例.2】查询当前表中f_name列所有水果名称,输入如下语句:

select f_name from fruits;

【例.3】例如,从fruits表中获取f_name和f_price两列,输入如下语句:

select f_name, f_price from fruits;

【例.4】查询价格为10.2元的水果的名称,输入如下语句:

select f_name, f_price
from fruits
where f_price = 10.2;

【例.5】查找名称为“apple”的水果的价格,输入如下语句:

select f_name, f_price
from fruits
where f_name = 'apple';

【例.6】查询价格小于10的水果的名称,输入如下语句:

select f_name, f_price
from fruits
where f_price < 10;

【例.7】s_id为101和102的记录,输入如下语句:

select s_id,f_name, f_price
from fruits
where s_id in (101,102)
order by f_name;

【例.8】查询所有s_id不等于101也不等于102的记录,输入如下语句:

select s_id,f_name, f_price
from fruits
where s_id not in (101,102)
order by f_name;

【例.9】查询价格在2.00元到10.5元之间水果名称和价格

select f_name, f_price
from fruits
where f_price between 2.00 and 10.20;

【例.10】查询价格在2.00元到10.5元之外的水果名称和价格

select f_name, f_price
from fruits
where f_price not between 2.00 and 10.20;

【例.11】查找所有以‘b'字母开头的水果,输入如下语句:

select f_id, f_name
from fruits
where f_name like 'b%';

【例.12】在fruits表中,查询f_name中包含字母‘g'的记录

select f_id, f_name
from fruits
where f_name like '%g%';

【例.13】查询以‘b'开头,并以‘y'结尾的水果的名称

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

【例7.14】在fruits表中,查询以字母‘y'结尾,且‘y'前面只有4个字母的记录

select f_id, f_name
from fruits
where f_name like '____y';

【例.15】查询customers表中c_email为空的记录的c_id、c_name和c_email字段值:

select c_id, c_name,c_email
from customers
where c_email is null;

【例.16】查询customers表中c_email不为空的记录的c_id、c_name和c_email字段值

select c_id, c_name,c_email
from customers
where c_email is not null;

【例.17】在fruits表中查询s_id = ‘101',并且f_price大于5的记录价格和名称

select f_id, f_price, f_name
from fruits
where s_id = '101' and f_price >=5;

【例.18】在fruits表中查询s_id = ‘101'或者'102',并且f_price大于5,并且f_name='apple'的记录价格和名称

select f_id, f_price, f_name
from fruits
where s_id in('101', '102') and f_price >= 5 and f_name = 'apple';

【例.19】查询s_id=101或者s_id=102的水果供应商的f_price和f_name,sql语句如下:

select s_id,f_name, f_price
from fruits
where s_id = 101 or s_id = 102;

【例.20】查询s_id=101或者s_id=102的水果供应商的f_price和f_name

select s_id,f_name, f_price
from fruits
where s_id in(101,102);

【例.21】查询fruits表中s_id字段的值,并返回s_id字段值不得重复

select distinct s_id from fruits;

【例.22】查询fruits表的f_name字段值,并对其进行排序

select f_name from fruits order by f_name;

【例.23】查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序

select f_name, f_price
from fruits
order by f_name, f_price;

【例.24】查询fruits表中的f_name和f_price字段,对结果按f_price降序方式排序

select f_name, f_price
from fruits
order by f_price desc;

【例.25】查询fruits表,先按f_price降序排序,再按f_name字段升序排序,sql语句如下:

select f_price, f_name
from fruits
order by f_price desc, f_name;

【例.26】根据s_id对fruits表中的数据进行分组

select s_id, count(*) as total
from fruits
group by s_id;

【例.27】根据s_id对fruits表中的数据进行分组,将每个供应商的水果名称显示出来

select s_id, group_concat(f_name) as names
from fruits
group by s_id;

【例.28】根据s_id对fruits表中的数据进行分组,并显示水果种类大于1的分组信息

select s_id, group_concat(f_name) as names
from fruits
group by s_id having count(f_name) > 1;

【例.29】根据s_id对fruits表中的数据进行分组,并显示记录数量

select s_id, count(*) as total
from fruits
group by s_id with rollup;

【例.30】根据s_id和f_name字段对fruits表中的数据进行分组, sql语句如下,

select * from fruits group by s_id,f_name;

【例.31】查询订单价格大于100的订单号和总订单价格

select o_num, sum(quantity * item_price) as ordertotal
from orderitems
group by o_num
having sum(quantity*item_price) >= 100;

【例.32】显示fruits表查询结果的前4行,输入如下语句:

select * from fruits limit 4;

【例.33】在fruits 表中,使用limit子句,返回从第5个记录开始的,行数长度为3的记录

select * from fruits limit 4, 3;

更多关于mysql相关内容感兴趣的读者可查看本站专题:《mysql常用函数大汇总》、《mysql日志操作技巧大全》、《mysql事务操作技巧汇总》、《mysql存储过程技巧大全》及《mysql数据库锁相关技巧汇总

希望本文所述对大家mysql数据库计有所帮助。