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

作者:程志伟 时间:2024-01-20 05:47:17 

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

来源:https://blog.csdn.net/c1z2w3456789/article/details/53896932

标签:MySQL,单表查询
0
投稿

猜你喜欢

  • MySQL中 LBCC 和 MVCC 的理解及常见问题示例

    2024-01-23 18:51:01
  • 初学ASP编程易犯的一个错误要注意

    2008-11-07 15:08:00
  • Python+Appium新手教程

    2022-12-01 04:12:44
  • Windows10下安装解压版MySQL教程图文详解

    2024-01-12 20:40:57
  • Python爬虫框架-scrapy的使用

    2022-09-11 20:12:28
  • 如何在pycharm中配置pyqt5设计GUI操作教程

    2021-09-28 17:42:21
  • 高效的SQLSERVER分页查询(推荐)

    2024-01-16 09:54:52
  • sqlserver isnull在数据库查询中的应用

    2011-12-01 10:30:25
  • Python中如何给字典设置默认值

    2023-09-21 00:15:32
  • SQL Server实现分页方法介绍

    2024-01-15 12:54:45
  • python集合的创建、添加及删除操作示例

    2022-07-09 13:29:38
  • Anaconda的安装及其环境变量的配置详解

    2022-08-31 19:21:11
  • 比较SQL Server约束和DML触发器

    2008-12-24 15:54:00
  • Python实现删除重复文件的示例代码

    2022-02-26 03:50:22
  • Python 如何截取字符函数

    2023-02-08 11:39:04
  • Python QTimer实现多线程及QSS应用过程解析

    2023-10-26 12:34:46
  • Python+Turtle实现绘制可爱的小仓鼠

    2022-09-16 20:52:28
  • 利用Python函数实现一个万历表完整示例

    2022-06-30 18:06:22
  • INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别

    2012-06-06 19:38:28
  • bootstrap 通过加减按钮实现输入框组功能

    2024-04-16 09:26:06
  • asp之家 网络编程 m.aspxhome.com