测试数据
create database cms
show databases
create table product(
productId int auto_increment,
productName varchar(255) not null,
salePrice int not null,
supplier varchar(255),
brand varchar(255) default '罗技',
cutoff double,
costPrice int ,
primary key(productId)
)
show tables
-- 添加一行数据
INSERT INTO `product` VALUES (1, '罗技M90', 90, '罗技', '罗技', 0.4, 35);
INSERT INTO `product` VALUES (2, '罗技M100', 49, '罗技', '罗技', 0.5, 36);
INSERT INTO `product` VALUES (3, '罗技M115', 99, '罗技', '罗技', 0.8, 37);
INSERT INTO `product` VALUES (4, '罗技M125', 80, '罗技', '罗技', 0.5, 38);
INSERT INTO `product` VALUES (5, '罗技木星轨迹球', 182, '罗技', '罗技', 0.7, 39);
INSERT INTO `product` VALUES (6, '罗技火星轨迹球', 349, '罗技', '罗技', 0.9, 290);
-- 查询用于数据测试
insert into product(productName,salePrice,supplier,brand,cutoff,costPrice)
select productName,salePrice,supplier,brand,cutoff,costPrice from product
select * from product -- 查看所有数据
-- 修改零售价
update product set productName='罗技M100',salePrice=49,costPrice=36 where productId=2;
update product set productName='罗技M115',salePrice=99,costPrice=37 where productId=3;
update product set productName='罗技M125',salePrice=80,costPrice=38 where productId=4;
update product set productName='罗技木星轨迹球',salePrice=182,costPrice=39 where productId=5;
update product set productName='罗技火星轨迹球',salePrice=349,costPrice=290 where productId=6;
1.基础查询
select 查询列表 from 表名;
/*
特点:
查询列表:表中字段、常量值、表达式、函数
查询结果:虚拟表格
如,字段与关键字重复使用 `name`区分
*/
-- 查询常量
select 100;
-- 查询表达式
select 100*10;
-- 查询函数
select version();
-- 别名:便于理解、字段重名,加以区分
1. 使用 as: select 100*10 as 结果
2. 使用 空格: select 100*10 结果
-- 别名与关键字重复,使用""
--去重
select distinct 字段 from 表名;
例如
SELECT DISTINCT cutoff FROM product;
-- MySQL中 + 号
+:运算符
100+90:两个数都为数值型,则做加法运算
'123'+90:其中一方为字符型,试图将字符型数值转换成数值型
成功则继续加减
失败则字符型为0
其中一方为null,则结果一直为null
-- 连接函数
select concat('a','b','c');
-- 结果为 abc
条件查询
select 查询列表 from 表名 where 筛选条件;
1.比较运算符
运算符 | 含义 |
= | 等于 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
!=(<>) | 不等于 |
-- 查询零售价为80的所有货物信息
select * from product where salePrice=80;
-- 查询名称为罗技M100的所有商品信息
select * from product where productName='罗技M100'
-- 查询名称不是罗技M100的所有商品信息
select * from product where productName !='罗技M100';
select * from product where productName <>'罗技M100'
-- 查询编号不是2的所有信息
select * from product where productId <> 2
-- 查询价格不小于150的 名称与编号
select productId,productName from product where salePrice > 150
-- 查询批发价大于50的商品
select * from product where costPrice>35
-- 别名问题
select productName 名称 from product
-- where 后不能使用别名
-- 先执行 where 后select
2.逻辑运算符
运算符 | 含义 |
and(&&) | 如果组合条件都是true,返回true |
or (||) | 如果组合条件一个为true,返回true |
not (!) | 如果条件为true,返回false ,取反 |
-- 选择Id,名称,批发价 批发价在150~400之间的货物
select productId,productName,salePrice from product
where salePrice>=150 and salePrice <=400;
-- 选择id为2,4的商品
select productId,productName from product
where productId=2 or productId=4;
-- 选择编号不为2的商品
select *from product where not productId=2;
select *from product where productId!=2;
-- 选择 零售价大于等于90或者进价大于等于200
select * from product where salePrice>=90 or costPrice>=200
3.优先级规则
-- 优先级问题
select * from product where(not productName like '%M%' and salePrice>100) or costPrice>40
4.范围查询_between and
-- where [列名] between [最小值] and [最大值]
select productId,productName,salePrice from product
where salePrice between 150 and 400
-- 类似于
select productId,productName,salePrice from product
where salePrice >150 and salePrice <400
-- 取反
select productId,productName,salePrice from product
where salePrice not between 150 and 400
5.集合查询_in
-- 集合查询 in
-- 选择id为2,4的商品
select productId,productName,salePrice from product
where productId in (1,5)
-- 选择id 不为2,4的商品
select productId,productName,salePrice from product
where productId not in (1,5)
6.空值查询_is null
-- 空值
select * from product where costPrice is null
insert into product(productName,salePrice)values('牧马人',200)
select * from product where costPrice is null
7.模糊查询_like
like通配符 | |
_ | 站一个位 |
% | 匹配所有 |
\ | 转义 |
$_ escape'$' | _ |
-- 模糊查询
select * from product where productName like '%星%'
百度搜索框不是模糊查询
8.判空函数
ifnull(字段,显示值);
ifnull(name,"佚名");