当前位置:Gxlcms > mysql > MySQL单表查询常见操作实例总结

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

时间:2021-07-01 10:21:17 帮助过:46人阅读

本文实例总结了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数据库计有所帮助。

您可能感兴趣的文章:

  • php+mysql+ajax实现单表多字段多关键词查询的方法
  • 解析mysql中:单表distinct、多表group by查询去除重复记录
  • MySQL单表多关键字模糊查询的实现方法
  • MySQL使用集合函数进行查询操作实例详解
  • MySQL使用正则表达式进行查询操作经典实例总结
  • mysql中模糊查询的四种用法介绍
  • Mysql联合查询UNION和UNION ALL的使用介绍
  • MySQL查询in操作 查询结果按in集合顺序显示
  • MySQL中基本的多表连接查询教程
  • mysql分页原理和高效率的mysql分页查询语句
  • 浅谈MySQL中优化sql语句查询常用的30种方法
  • mysql查询昨天 一周前 一月前 一年前的数据

人气教程排行