查询是数据库管理系统中一个重要功能,数据查询不应只是简单返回数据库中存储的信息
还应该根据需要对数据进行筛选以及确定数据以什么形式显示
select * from 表名;
select 列名1,列名2 from 表名;
select * from 表名 as 别名;
select 列名1 as 别名,列名2 as 别名 ,from 表名;
当所有列均相同时才会去重复值(一般不会出现---主键)
select distinct 列名 from 表名;
select 列名 ,列名+10 别名 from 表名;
用到了别名 运算结果是+10后显示别名 不会修改原表数据;
运算符概念:数据库中的表结构确定后,表中数据代表的意义就已经确定,通过mysql运算符进行运算就可以获取到表结构以外的另一种数据。
下边以实例演示运算符的应用
select pname ,price*1.1 as new_price from 表名;
select *from 表名 where pname='海尔洗衣机';
(1)select *from 表名 where price between 100 and 200;
(2) select *from 表名 where price >=100 and price<=200;
select *from 表名where price in(200,800);
in 关键字括号中的值满足一个就好
(1)查询含有’裤‘字的所以商品:
select * from 表名 where pname like '%裤%‘;
(2)查询以’海‘字开头的所以商品:
select * from 表名 where pname like '海%‘;
(3)查询第二个字为’寇‘的所有商品:
select * from 表名 where pname like '-寇%‘;
NULL 不能用= 因为NULL不等于任何值(包括它自己)
查询id不为NULL 的商品
select * from 表名 where列名 is not NULL;
(1)least求最小值:
select least(5,10,20); select least(5,10,20)as smallnumber; select least(5,null20);
如果数值中有NULL 则不会进行比较 直接输出NULL
(2)greatest求最大值
select greatest (10,30,20)as bignumber; select greatest (10,null,20)as bignumber;
如果数值中有NULL 则不会进行比较 直接输出NULL