澳门皇冠金沙网站▎在线官网
做最好的网站

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

2020-04-21 作者:网络服务   |   浏览(162)
SELECT AVG(f_price) AS avg_price FROM fruits WHERE s_id = 103;SELECT AVG(f_price) AS avg_price FROM fruits group by s_id;

查询fruits表的f_name字段值,并对其进行排序

在fruits表中查找f_name的最大值,SQL语句如下

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

SELECT COUNT(*) AS cust_num from customers;

在fruits表中,查询f_name中包含字母‘g'的记录

SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
SELECT s_id, GROUP_CONCAT(f_name) AS NamesFROM fruitsGROUP BY s_id;

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

在fruits 表中,使用LIMIT子句,返回从第5个记录开始的,行数长度为3的记录

SELECT s_id,AVG(f_price) AS avg_priceFROM fruitsGROUP BY s_id;
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 SUM(quantity) AS items_total FROM orderitems WHERE o_num = 30005;SELECT o_num, SUM(quantity) AS items_total FROM orderitems GROUP BY o_num;

从第五行开始,读取3行

在fruits表中查找不同供应商提供的价格最高的水果

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

SELECT COUNT(*) AS cust_num from customers;SELECT COUNT(c_email) AS email_num FROM customers;SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
SELECT DISTINCT s_id FROM fruits;select f_name from fruits ORDER BY f_name;

在orderitems表中查询30005号订单一共购买的水果总量,输入如下语句:

SELECT c_id, c_name,c_emailFROM customersWHERE c_email IS NOT NULL;

AVG函数

使用in操作更加简洁明了

查询customers表中有电子邮箱的顾客的总数,输入如下语句:

SELECT s_id, COUNT(*) AS TotalFROM fruitsGROUP BY s_id;

SUM函数

SELECT f_id, f_price, f_nameFROM fruitsWHERE s_id = '101' AND f_price =5;

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

SELECT f_name FROM fruits;
mysqlSELECT MAX(f_price) AS max_price FROM fruits;

查询以‘b'开头,并以‘y'结尾的水果的名称

在fruits表中查找市场上价格最高的水果,SQL语句如下:

SELECT o_num, SUM(quantity * item_price) AS orderTotalFROM orderitemsGROUP BY o_numHAVING SUM(quantity*item_price) = 100;SELECT o_num, SUM(quantity * item_price) AS orderTotalFROM orderitemsGROUP BY o_numHAVING SUM(quantity*item_price) = 100order by ordertotal;SELECT * From fruits LIMIT 8;
mysqlSELECT MIN(f_price) AS min_price FROM fruits;

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

SELECT MAX(f_name) from fruits;

字段不重复

本文实例讲述了MySQL使用集合函数进行查询操作。分享给大家供大家参考,具体如下:

创建customers表:

SELECT MIN(f_price) AS min_price FROM fruits;SELECT s_id, MIN(f_price) AS min_price FROM fruits GROUP BY s_id;

从fruits表中检索所有字段的数据

SELECT o_num, SUM(quantity) AS items_totalFROM orderitemsGROUP BY o_num;
SELECT f_name, f_priceFROM fruitsORDER BY f_name, f_price;

在fruits表中查找市场上价格最低的水果,SQL语句如下:

s_id为101和102的记录,输入如下语句:

查询customers表中总的行数

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

SELECT s_id, MIN(f_price) AS min_priceFROM fruitsGROUP BY s_id;
SELECT s_id,f_name, f_priceFROM fruitsWHERE s_id = 101 OR s_id = 102;

在fruits表中,查询s_id=103的供应商的水果价格的平均值,SQL语句如下:

查询价格在2.00元到10.5元之外的水果名称和价格

在fruits表中查找不同供应商提供的价格最低的水果

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

SELECT AVG(f_price) AS avg_priceFROM fruitsWHERE s_id = 103;

使用having过滤分组

COUNT函数

SELECT s_id, GROUP_CONCAT(f_name) AS NamesFROM fruitsGROUP BY s_id having count(f_name)1;
SELECT SUM(quantity) AS items_totalFROM orderitemsWHERE o_num = 30005;
SELECT f_id, f_nameFROM fruitsWHERE f_name LIKE '%g%';

MAX函数

查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序

在orderitems表中,使用SUM()函数统计不同订单号中订购的水果总量

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

在fruits表中,查询每一个供应商的水果价格的平均值,SQL语句如下:

SELECT f_id, f_nameFROM fruitsWHERE f_name LIKE '____y';
SELECT s_id, MAX(f_price) AS max_priceFROM fruitsGROUP BY s_id;
SELECT f_name, f_priceFROM fruitsWHERE f_price  10;

MIN函数

SELECT * From fruits LIMIT 4,3;
SELECT MAX(f_price) AS max_price FROM fruits;SELECT s_id, MAX(f_price) AS max_price FROM fruits GROUP BY s_id;SELECT MAX(f_name) from fruits;
SELECT f_name, f_priceFROM fruitsWHERE f_price NOT BETWEEN 2.00 AND 10.20;

在orderitems表中,使用COUNT()函数统计不同订单号中订购的水果种类

根据s_id和f_name字段对fruits表中的数据进行分组, SQL语句如下,

SELECT COUNT(c_email) AS email_numFROM customers;

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

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

SELECT f_name, f_priceFROM fruitsWHERE f_price = 10.2;
SELECT f_name, f_priceFROM fruitsORDER BY f_price DESC;

常用查询语句:

SELECT f_id, f_nameFROM fruitsWHERE f_name LIKE 'b%';
SELECT s_id, COUNT(*) AS TotalFROM fruitsGROUP BY s_id WITH ROLLUP;
SELECT s_id,f_name, f_priceFROM fruitsWHERE s_id IN (101,102)ORDER BY f_name;
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;SELECT * from fruits group by s_id,f_name;

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

创建fruits表:

SELECT f_nameFROM fruitsWHERE f_name LIKE 'b%y';

本文由澳门皇冠金沙网站发布于网络服务,转载请注明出处:MySQL单表查询常见操作实例总结

关键词: