mysql数据库栏目_mysql数据库的基础(二)

2024-01-25 07:10

本文主要是介绍mysql数据库栏目_mysql数据库的基础(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

查询

select 列1,列2,列3 from 表 where 条件

select查询模型

列是变量

条件可以理解为表达式

比如:

select goods_id,goods_name,shop_price,shop_market_price,shopmarketprice - shopprice from goods where shopmarketprice - shopprice>10

条件查询

我们查询的时候可以加上where条件这样既能取得我们想要的数据

125f2a049c948f732c7d275714ed1db8.png

这里做一个简单查询练习

b07773cd6fedc1b6e439ab7a827f1535.png

查出满足以下条件的商品

1.1:主键为32的商品

select goods_id,goods_name,shop_price from ecs_goods where goods_id=32;

2.1.2:不属第3栏目的所有商品

select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id!=3;

2.1.3:本店价格高于3000元的商品

select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price >3000;

2.1.4:本店价格低于或等于100元的商品

select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price <=100;

2.1.5:取出第4栏目或第11栏目的商品(不许用or)

select goods_id,cat_id,goods_name,shop_price from ecs_goods

where cat_id in (4,11);

2.1.6:取出100<=价格<=500的商品(不许用and)

select goods_id,cat_id,goods_name,shop_price from ecs_goods

where shop_price between 100 and 500;

2.1.7:取出不属于第3栏目且不属于第11栏目的商品(and,或not in分别实现)

select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id!=3 and cat_id!=11;

select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id not in (3,11);

2.1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品(and的优先级高于or)

select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price>100 and shop_price <300 or shop_price >4000 and shop_price <5000;

2.1.9:取出第3个栏目下面价格<1000或>3000,并且点击量>5的系列商品(注意and的优先级高于or)

select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods where

cat_id=3 and (shop_price <1000 or shop_price>3000) and click_count>5;

2.1.10:取出第1个栏目下面的商品(注意:1栏目下面没商品,但其子栏目下有)

select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods

where cat_id in (2,3,4,5);

809d8001d8270b7613e017ee8ddfa84b.png

2.1.11:取出名字以"诺基亚"开头的商品

select goods_id,cat_id,goods_name,shop_price from ecs_goods where goods_name like '诺基亚%';

2.1.12:取出名字为"诺基亚Nxx"的手机

select goods_id,cat_id,goods_name,shop_price from ecs_goods

where goods_name like '诺基亚N__';

2.1.13:取出名字不以"诺基亚"开头的商品

select goods_id,cat_id,goods_name,shop_price from ecs_goos

where goods_name not like '诺基亚%';

2.1.14:取出第3个栏目下面价格在1000到3000之间,并且点击量>5 "诺基亚"开头的系列商品

select goods_id,cat_id,goods_name,shop_price from ecs_goods where

cat_id=3 and shop_price>1000 and shop_price <3000 and click_count>5 and goods_name like '诺基亚%';

select goods_id,cat_id,goods_name,shop_price from ecs_goods where

shop_price between 1000 and 3000 and cat_id=3 and click_count>5 and goods_name like '诺基亚%';

2.1.15把num值处于[20,29]之间,改为20,num值处于[30,39]之间的,改为30

update table set num=floor(num/10)*10 where num >=20 and num <=39

2.1.16把good表中商品名为'诺基亚xxxx'的商品,改为'HTCxxxx',

update goods set goods_name=concat("HTC",substring(goods_name,4)) where goods_name like "诺基亚%"

奇怪的null

当我们的某个字段设置为可以为空,我们插入数据时可以插入null。

select from 表 where 字段 != null 这样是查询不到任何数据的

select from 表 where 字段 = null 这样是查询不到任何数据的

注意在mysql数据库中null是不等于null

当我们要想查字段是否是null专门提供一个查询条件 is null 如下

select * from 表 where 字段 is null 这样能查出来字段为null的值

这种查询方式不利于优化,所以不推荐使用,我们建立字段是可以设置成不为空再加上一个默认值,默认值为‘’空字符串。

聚合函数

聚合函数(常用于group by从句的select查询中)

avg(col)返回指定列的平均值

count(col)返回指定列中非null值的个数

min(col)返回指定列的最小值

max(col)返回指定列的最大值

sum(col)返回指定列的所有值之和

group_concat(col) 返回由属于一组的列值连接组合而成的结果

2.1:查出最贵的商品的价格

select max(shop_price) from ecs_goods;

2.2:查出最大(最新)的商品编号

select max(goods_id) from ecs_goods;

2.3:查出最便宜的商品的价格

select min(shop_price) from ecs_goods;

2.4:查出最旧(最小)的商品编号

select min(goods_id) from ecs_goods;

2.5:查询该店所有商品的库存总量

select sum(goods_number) from ecs_goods;

2.6:查询所有商品的平均价

select avg(shop_price) from ecs_goods;

2.7:查询该店一共有多少种商品

select count(*) from ecs_goods;

分组查询

select 聚合函数,分组字段,【其他字段】 from 表 group by 分组字段

group by 时会mysql内部会先根据分组字段排序然后再统计,我们可以给分组字段设置索引。

我们分组查询时我们查找的字段有聚合函数,分组字段,这都是没有问题的,但是有个其他字段是有问题的,这个其他字段到底是那条记录的值呢?就好比我让大家按宿舍统计宿舍人的平均身高,这时我们查询的宿舍号和平均身高都没有问题,但是你又要了姓名,这时该用谁的姓名呢?其实这种写法语义上是有问题的,在orancle上直接会报错,但是mysql上不会报错,这时这个其他字段会使用第一次出现的那一行的那个列来填充。

查询每个栏目下面最贵商品价格

select cat_id,max(shop_price) from ecs_goods group by cat_id;

group_concat的使用

b9747b683b6e3b05e3d10f8b05bc9520.png

155e7bf9e323497348b3ab966e9eac31.png

having筛选

我们要想查询市场价比本店高超过200个商品

select goods_id,goods_name,market_price-shop_price

from ecs_goods where market_price-shop_price > 200 ;

上面的写法会计算市场价和本店价差值计算2次

我们可以使用一个别名的方式

select goods_id,goods_name,market_price-shop_price as j

from ecs_goods where j > 200 ;

上面的写法会报错,说不认识 j 这个字段。

这时我们不能使用where了我们得使用having

select goods_id,goods_name,market_price-shop_price as j

from ecs_goods having j > 200 ;

where和having的区别

where是针对磁盘上的数据进行筛选,筛选出结果集后放在内存中我们再使用having进行筛选。having永远不能放在where前面。

分组查询练习

3 having与group综合运用查询:

3.1:查询该店的商品比市场价所节省的价格

select goods_id,goods_name,market_price-shop_price as j

from ecs_goods ;

3.2:查询每个商品所积压的货款(提示:库存单价)

select goods_id,goods_name,goods_numbershop_price from ecs_goods

3.3:查询该店积压的总货款

select sum(goods_number*shop_price) from ecs_goods;

3.4:查询该店每个栏目下面积压的货款.

select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id;

3.5:查询比市场价省钱200元以上的商品及该商品所省的钱(where和having分别实现)

select goods_id,goods_name,market_price-shop_price as k from ecs_goods

where market_price-shop_price >200;

select goods_id,goods_name,market_price-shop_price as k from ecs_goods

having k >200;

3.6:查询积压货款超过2W元的栏目,以及该栏目积压的货款

select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id having k>20000

排序order by

order by 字段 不写就是正序

order by 字段 asc 正序

order by 字段 desc 倒叙

order by 字段1 正序,字段2 desc 先按照字段1进行正序排,如果字段1都一样的话,再根据字段2进行倒叙排列。

限制条数limit

limit 3 取前3条

limit 5,3 从第5条取,取3条。

select 字段1,字段2 ,聚合函数 from 表 where 条件 group by 字段 having 条件 order by 字段 limit 便宜量,限制条数

3.7:where-having-group综合练习题

有如下表及数据

+------+---------+-------+

| name | subject | score |

+------+---------+-------+

| 张三 | 数学 | 90 |

| 张三 | 语文 | 50 |

| 张三 | 地理 | 40 |

| 李四 | 语文 | 55 |

| 李四 | 政治 | 45 |

| 王五 | 政治 | 30 |

+------+---------+-------+

要求:查询出2门及2门以上不及格者的平均成绩

一种错误做法

mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;

+------+---+------------+

| name | k | avg(score) |

+------+---+------------+

| 张三 | 3 | 60.0000 |

| 李四 | 2 | 50.0000 |

+------+---+------------+

2 rows in set (0.00 sec)

#利用having筛选挂科2门以上的.

mysql> select name,sum(score < 60) as gk ,avg(score) as pj from stu group by name having gk >=2;

+------+------+---------+

| name | gk | pj |

+------+------+---------+

| 张三 | 2 | 60.0000 |

| 李四 | 2 | 50.0000 |

+------+------+---------+

2 rows in set (0.00 sec)

子查询

where 型子查询

将查询的结果作为另一条查询语句的查询条件

查询goods_id最大的那条数据

select from goods where goods_id=(select max(goods_id) from goods);

查询每个分类下goods_id最大的那个商品的信心

select from goods where goods_id in (select max(goods_id) from goods group by cate_id);

from型子查询

将查询的结果集作为另一条查询语句的表来看

select from (select from goods order by cate_id asc,goods_id desc) group by cate_id

exists型的子查询

查出栏目下没有商品的栏目

select from cate where exists(select from goods where

goods.cate_id=cate.cate_id;

连表查询

内连接

select from goods inner join cate on goods.cate_id = cate.cate_id

inner 可以省略

如果goods表中的数据在cate表中匹配不到数据时,就把goods表条这数据就丢掉。

左(外)连接

select from goods left join cate on goods.cate_id = cate.cate_id

如果goods表中的数据在cate表中匹配不到数据时,这是会把goods表中的数据留下来,cate表的字段用null进行填充

右(外)连接

select from goods right join cate on goods.cate_id = cate.cate_id

如果goods表中的数据在cate表中匹配不到数据时,会把cate表中的数据留下来,goods表的字段用null进行填充。

全(外)连接

select from goods full join cate on goods.cate_id = cate.cate_id

如果goods表中的数据在cate表中匹配不到数据时,会把goods表中的数据留下来,cate表的字段用null进行填充。如果cate表中的数据在goods表中匹配不到数据时,会把cate表中的数据留下来,goods表的字段用null进行填充。

交叉查询

select from 表1 cross join 表2

获得表1和表2的笛卡尔积

简写形式 select from 表1,表2

联合查询

将查询的结果集纵向的合并为一个结果集

select goods_id,goods_name from goods where cate_i=2

unnin

select goods_id,goods_name from goods where cate_id=4

数据源可以是同一张表,亦可以是不同表,各个语句取出的列数必须相同,字段名称用的是第一个sql语句的字段。注意上面的数据有完全相同的会被合并(去重),合并和花费大量的时间

我们可以使用 union all 这样就不会将相同的数据合并(不会去重)

select goods_id,goods_name from goods where cate_i=2

unnin all

select goods_id,goods_name from goods where cate_id=4

union联合查询时每个sql语句排序是没有意义的。

这篇关于mysql数据库栏目_mysql数据库的基础(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/642470

相关文章

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

mysql外键创建不成功/失效如何处理

《mysql外键创建不成功/失效如何处理》文章介绍了在MySQL5.5.40版本中,创建带有外键约束的`stu`和`grade`表时遇到的问题,发现`grade`表的`id`字段没有随着`studen... 当前mysql版本:SELECT VERSION();结果为:5.5.40。在复习mysql外键约

SQL注入漏洞扫描之sqlmap详解

《SQL注入漏洞扫描之sqlmap详解》SQLMap是一款自动执行SQL注入的审计工具,支持多种SQL注入技术,包括布尔型盲注、时间型盲注、报错型注入、联合查询注入和堆叠查询注入... 目录what支持类型how---less-1为例1.检测网站是否存在sql注入漏洞的注入点2.列举可用数据库3.列举数据库

数据库oracle用户密码过期查询及解决方案

《数据库oracle用户密码过期查询及解决方案》:本文主要介绍如何处理ORACLE数据库用户密码过期和修改密码期限的问题,包括创建用户、赋予权限、修改密码、解锁用户和设置密码期限,文中通过代码介绍... 目录前言一、创建用户、赋予权限、修改密码、解锁用户和设置期限二、查询用户密码期限和过期后的修改1.查询用

Mysql虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

mysql数据库分区的使用

《mysql数据库分区的使用》MySQL分区技术通过将大表分割成多个较小片段,提高查询性能、管理效率和数据存储效率,本文就来介绍一下mysql数据库分区的使用,感兴趣的可以了解一下... 目录【一】分区的基本概念【1】物理存储与逻辑分割【2】查询性能提升【3】数据管理与维护【4】扩展性与并行处理【二】分区的

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat