[mysql] select查询语句大全指南

有关mysql其他的命令语句可点击此处获取


本博客使用如下表结构作为例子讲解

create table commoditytype(
				ct_id int primary key auto_increment,
				ct_name varchar(50) not null
			)default charset=utf8;
			
	create table commodity(
				c_id int primary key auto_increment,
				c_name varchar(50) not null,
				c_madein varchar(50) not null,
				c_type int,
				constraint fk_1  foreign key  (c_type)  references   commoditytype(ct_id),
				c_inprice int not null,
				c_outprice int not null,
				c_num int default 100
			)default charset=utf8;
		

单表查询:

  • 查询所有数据:
    select * from 表名称

  • 查询某些字段:
    select c_id,c_name from commodity;

  • 查询常量:

    ->>select ‘hello’;
    ->>select ‘hello’ as ‘你好’;

  • 自定义显示名称:
    如:查询商品的名称,进价和售价
    –>>select c_name as ‘商品名称’,c_inprice as ‘进价’,c_outprice as ‘售价’ from commodity;

  • 查询时外键作为条件:
    如:查询所有玩具
    ->>select * from commodity where c_type=(select ct_id from commoditytype where ct_name=‘玩具’);

  • 查询 所有商品的类型 筛选后的数据
    distinct 用于去重

    没有去重效果
    -> select c_type as ‘类型’ from commodity;
    有去重效果
    -> select distinct c_type as ‘类型’ from commodity;


  • 四则运算
    如:想看看每个商品 利润。显示 名称,进价,售价,利润。
    -> select c_name as ‘商品名称’,c_inprice as ‘进价’,c_outprice as ‘售价’,(c_outprice-c_inprice) as ‘利润’ from commodity;

    如:想看看每个商品 总成本。显示 名称 ,总成本。
    -> select c_name as ‘商品名称’,c_inprice*c_num as ‘总成本’ from commodity;

  • 比较运算:
    < ,> ,= , >= , <=, !=

    如:查询 进价大于 100 的
    -> select c_name,c_inprice from commodity where c_inprice >100;

    如:查询利润 大于100的
    -> select c_name as ‘商品名称’,c_inprice as ‘进价’,c_outprice as ‘售价’,(c_outprice-c_inprice) as ‘利润’ from commodity where (c_outprice-c_inprice)>100;

    如:查询利润大于成本的
    -> select c_name as ‘商品名称’,c_inprice as ‘进价’,(c_outprice-c_inprice) as ‘利润’ from commodity where (c_outprice-c_inprice)>c_inprice;

    -> select c_name as ‘商品名称’,c_inprice as ‘进价’,(c_outprice-c_inprice) as ‘利润’ from commodity where (c_outprice-c_inprice)/c_inprice>1;

  • 逻辑运算
    and or not
    如:查询产地是中国且库存大于50 的
    -> select c_name,c_madein,c_num from commodity where c_madein=‘中国’ and c_num>50;

    如:查询产地是 中国且库存在 50-100 ,包括临界点 的
    -> select c_name,c_madein,c_num from commodity where c_madein=‘中国’ and c_num>=50 and c_num<=100;

    如:在什么之间 between 50 and 100
    -> select c_name,c_madein,c_num from commodity where c_madein=‘中国’ and c_num between 50 and 100;

    如:不在什么之间 not between 50 and 100
    -> select c_name,c_madein,c_num from commodity where c_madein=‘中国’ and c_num not between 50 and 100;

    如:查询 商品类型是 服装 或 食品 或 玩具 的
    -> select c_id,c_name,c_type from commodity where c_type=4 or c_type=5 or c_type=1;

    相当于:
    -> select c_id,c_name,c_type from commodity where c_type in (1,4,5);

  • 值的匹配 like (模糊查询)

    下划线 _ 表示一个字符
    百分号 % 表示多个字符

    如: _小 表示两个字符 x小
    %小 表示多个字符 xxx小
    %小% 表示 字符中有 小 字

    如:查询 产地 是 _国 的
    -> select * from commodity where c_madein like “_国”;

    如:查询商品 结尾 是 ‘小’ 字的
    -> select c_name from commodity where c_name like “%小”;

    如:查询商品 中 包含 ‘具’ 字的
    -> select c_name from commodity where c_name like “%具%”;

    如:查询商品 开头 是 ‘乐’ 字的
    -> select c_name from commodity where c_name like “乐%”;

    如:查询商品 中包含 ‘中’ 和 ‘H’ 的
    -> select c_name from commodity where c_name like “%中%” and c_name like “%H%”;


  • 排序
    语法: order by 列名称 [asc/desc]
    默认升序 order by 列 asc (ascending)
    降序 order by 列 desc(descending)

    如:查询所有商品的售价,降序排序
    -> select c_name,c_outprice from commodity order by c_outprice desc;

    如:查询产地是中国的 所有商品的售价,降序排序
    -> select c_name,c_madein,c_outprice from commodity where c_madein=“中国” order by c_outprice desc;

  • 限制查询条数
    语法: limit [起始值,]总条数
    起始值: 默认0
    limit 3 从第一条开始取3条
    limit 2,4 从第三条开始,取4条
    例:
    ->select c_id,c_name from commodity limit 3;


函数

  • count( xxx ) 计算 xxx 的数量
    如:得到 所有数据 的 数量
    -> select count() from commodity;
    如:数量是可以直接拿来计算的
    -> select (select count(
    ) from commodity) * 2;
    如:查询所有玩具的数量
    -> select count(*) from commodity where c_type=1;
    -> select count(c_name) as “玩具数量” from commodity where c_type=1;

  • 平均值 avg()

  • 求和 sum()

  • 最大值 max()

  • 最小值 min()

    如:进价的均值
    -> select avg(c_inprice) as “进价均值” from commodity;
    如:售价的最大值
    -> select max(c_outprice) as “售价最大值” from commodity;
    如:售价的最小值
    -> select min(c_outprice) as “售价最小值” from commodity;
    如:所有商品的 数量
    -> select sum(c_num) as “商品数量” from commodity;
    如:显示 玩具 数量
    -> select sum(c_num) as “玩具数量” from commodity where c_type=1;

    注意:
    这些函数都是默认 is not null 处理的
    如果某个参与的值 本身 为 null,那么计算的时候是不考虑的


  • 分组
    语法: group by 列名称.
    对关键字进行分组,通常 和 聚合函数 一起使用

    如:按照 种类 进行 分类,看每个种类的数量
    -> select c_type,count(c_type) from commodity group by c_type;

    如:每类商品的平均 进价
    -> select c_type,avg(c_inprice) from commodity group by c_type;

    如:每类商品中售价 最贵的最平移的
    -> select max(c_outprice),min(c_outprice),c_type from
    commodity group by c_type;

  • having 关键词
    where 条件筛选针对已有数据,也就是原数据
    having 条件筛选 聚合函数处理后的数据
    where 优先级高于having

    如:安照商品类型,看均价
    -> select c_type,avg(c_inprice) from commodity group by c_type;

    如:安照商品类型,看均价 大于 50的
    -> select c_type,avg(c_inprice) from commodity group by c_type having avg(c_inprice)>50;

    如:选取进价 大于 100 的商品,分类后,求均值
    -> select c_type,avg(c_inprice) from commodity where c_inprice>100 group by c_type;


联表查询 ,多表查询
在前面查询中, 商品类型其实是用数字代替的。而且,我们是直接使用 1,2,3,4,5 的。在实际中我们不知道 具体的数字,只知道是什么商品,如玩具 ,那么如何从 commodity 中 查询出 所有玩具?

  • 内连接查询:
    语法:select xxx from 表1 inner join 表2 on 条件

    如:查询出所有玩具
    -> select * from commodity inner join commoditytype
    on c_type=ct_id where ct_name=‘玩具’;

    如:还可以只显示每张表的部分内容

    -> select c_id,c_name,ct_name from commodity inner join commoditytype on c_type=ct_id where ct_name=‘玩具’;

  • 外连接查询:
    语法:select xxx from 表1 left/right join 表2 on 条件
    left join 左连接
    right join 右连接
    left 以做为主表,right以右边为主表
    主表会全部显示,查不到的显示 null

    如:去类型表增加几个类型

    如:以 commodity 为主
    -> select * from commodity left join commoditytype on c_type=ct_id;

    如:以 commoditytype 为主
    -> select * from commodity right join commoditytype on c_type=ct_id;

    如:把类型放在左侧,自定义显示
    -> select c_id,ct_id,ct_name,c_name from commodity right join commoditytype on c_type=ct_id;

    如:换下表的位置
    -> select * from commoditytype left join commodity on c_type=ct_id;


  • 子查询
    就是 把某个表中查出来的数据作为我们的查询条件

    • 案例1:
      1.1 问书籍的 ct_id 是多少?
      -> select ct_id from commoditytype where ct_name=‘书籍’;

      1.2 我们知道 书籍对应着3,我们怎么从 commodity中查出所有书籍
      -> select * from commodity where c_type=3;

      1.3 更多的时候不知道直接的数, 把1.1 和 1.2 写一起,即子查询
      -> select * from commodity where c_type=(select ct_id from commoditytype where ct_name=‘书籍’);

    • 案例2:
      书籍的平均售价。 最终显示类型和价格
      -> select c_type,avg(c_outprice) from commodity where c_type=(
      select ct_id from commoditytype where ct_name=‘书籍’);

    • 案例3:售价大于 书籍均价的所有商品。
      3.1 假设均价是40.
      select * from commodity where c_outprice>40;

      3.2 求书籍售价均价
      select avg(c_outprice) from commodity where c_type=3;

      3.3 书籍的id
      select ct_id from commoditytype where ct_name=‘书籍’;

      优化:select * from commodity where c_outprice>(select avg(c_outprice) from commodity where c_type=(select ct_id from commoditytype where ct_name=‘书籍’));


附上commodity的数据集。

INSERT INTO commodity VALUES (‘1’, ‘变形金刚-擎天柱’, ‘中国’, ‘1’, ‘20’, ‘50’, ‘60’);
INSERT INTO commodity VALUES (‘2’, ‘变形金刚-霸天虎’, ‘中国’, ‘1’, ‘20’, ‘45’, ‘50’);
INSERT INTO commodity VALUES (‘3’, ‘变形金刚-威震天’, ‘美国’, ‘1’, ‘120’, ‘245’, ‘15’);
INSERT INTO commodity VALUES (‘4’, ‘魔仙玩偶1’, ‘中国’, ‘1’, ‘6’, ‘12’, ‘100’);
INSERT INTO commodity VALUES (‘5’, ‘超人玩偶’, ‘中国’, ‘1’, ‘29’, ‘99’, ‘100’);
INSERT INTO commodity VALUES (‘7’, ‘小霸王游戏机’, ‘中国’, ‘1’, ‘50’, ‘99’, ‘300’);
INSERT INTO commodity VALUES (‘8’, ‘X-BOX游戏机’, ‘美国’, ‘1’, ‘1200’, ‘3000’, ‘12’);
INSERT INTO commodity VALUES (‘9’, ‘任天堂游戏机’, ‘日本’, ‘1’, ‘300’, ‘600’, ‘30’);
INSERT INTO commodity VALUES (‘10’, ‘乐高玩具-散装’, ‘中国’, ‘1’, ‘40’, ‘60’, ‘100’);
INSERT INTO commodity VALUES (‘11’, ‘乐高玩具-快乐家庭’, ‘中国’, ‘1’, ‘50’, null, ‘20’);
INSERT INTO commodity VALUES (‘12’, ‘乐高玩具-蝙蝠侠纪念版’, ‘新加坡’, ‘1’, ‘290’, ‘590’, ‘6’);
INSERT INTO commodity VALUES (‘13’, ‘夏日有人手办’, ‘中国’, ‘1’, ‘20’, ‘50’, ‘10’);
INSERT INTO commodity VALUES (‘14’, ‘EVA模型玩具’, ‘日本’, ‘1’, ‘200’, ‘450’, ‘10’);
INSERT INTO commodity VALUES (‘15’, ‘平板电脑模型’, ‘中国’, ‘1’, ‘2’, ‘12’, ‘120’);
INSERT INTO commodity VALUES (‘16’, ‘手机模型玩具’, ‘中国’, ‘1’, ‘7’, ‘24’, ‘60’);
INSERT INTO commodity VALUES (‘17’, ‘手机模型玩具’, ‘美国’, ‘1’, ‘20’, ‘80’, ‘10’);
INSERT INTO commodity VALUES (‘18’, ‘手机模型玩具’, ‘日本’, ‘1’, ‘40’, null, ‘8’);
INSERT INTO commodity VALUES (‘19’, ‘hellokitty手机链’, ‘中国’, ‘1’, ‘20’, ‘60’, ‘40’);
INSERT INTO commodity VALUES (‘20’, ‘水枪-小’, ‘中国’, ‘1’, ‘2’, ‘4’, ‘80’);
INSERT INTO commodity VALUES (‘21’, ‘水枪-大’, ‘中国’, ‘1’, ‘10’, ‘18’, ‘80’);
INSERT INTO commodity VALUES (‘22’, ‘水枪-小’, ‘中国’, ‘1’, ‘2’, ‘4’, ‘80’);
INSERT INTO commodity VALUES (‘23’, ‘中华铅笔HB’, ‘中国’, ‘2’, ‘1’, ‘2’, ‘100’);
INSERT INTO commodity VALUES (‘24’, ‘中华铅笔2B’, ‘中国’, ‘2’, ‘1’, ‘2’, ‘109’);
INSERT INTO commodity VALUES (‘25’, ‘中华铅笔2H’, ‘中国’, ‘2’, ‘1’, ‘2’, ‘100’);
INSERT INTO commodity VALUES (‘26’, ‘超人图案笔袋’, ‘中国’, ‘2’, ‘6’, ‘12’, ‘12’);
INSERT INTO commodity VALUES (‘27’, ‘中华绘图橡皮’, ‘中国’, ‘2’, ‘1’, ‘2’, ‘100’);
INSERT INTO commodity VALUES (‘28’, ‘西瓜图案铅笔盒’, ‘中国’, ‘2’, ‘12’, ‘20’, ‘50’);
INSERT INTO commodity VALUES (‘29’, ‘hellokitty文具礼盒套装’, ‘中国’, ‘2’, ‘33’, ‘99’, ‘10’);
INSERT INTO commodity VALUES (‘30’, ‘简易卷笔刀’, ‘中国’, ‘2’, ‘1’, ‘2’, ‘100’);
INSERT INTO commodity VALUES (‘31’, ‘自动卷笔刀’, ‘中国’, ‘2’, ‘6’, ‘20’, ‘40’);
INSERT INTO commodity VALUES (‘32’, ‘礼盒丝带-红’, ‘中国’, ‘2’, ‘2’, ‘6’, ‘80’);
INSERT INTO commodity VALUES (‘33’, ‘礼盒丝带-蓝’, ‘中国’, ‘2’, ‘2’, ‘6’, ‘40’);
INSERT INTO commodity VALUES (‘34’, ‘蝴蝶图案直尺’, ‘中国’, ‘2’, ‘14’, ‘26’, ‘44’);
INSERT INTO commodity VALUES (‘35’, ‘折叠尺’, ‘中国’, ‘2’, ‘20’, ‘60’, ‘4’);
INSERT INTO commodity VALUES (‘36’, ‘超人图案书包’, ‘中国’, ‘2’, ‘60’, ‘160’, ‘3’);
INSERT INTO commodity VALUES (‘37’, ‘公主图案书包’, ‘中国’, ‘2’, ‘60’, ‘160’, ‘6’);
INSERT INTO commodity VALUES (‘38’, ‘熊大图案拉杆箱-小’, ‘中国’, ‘2’, ‘160’, ‘260’, ‘1’);
INSERT INTO commodity VALUES (‘39’, ‘儿童彩色铅笔’, ‘中国’, ‘2’, ‘60’, ‘80’, ‘13’);
INSERT INTO commodity VALUES (‘40’, ‘快乐家族文具礼盒’, ‘中国’, ‘2’, ‘160’, ‘460’, ‘2’);
INSERT INTO commodity VALUES (‘41’, ‘java入门到精通’, ‘中国’, ‘3’, ‘30’, ‘66’, ‘15’);
INSERT INTO commodity VALUES (‘42’, ‘.net入门到精通’, ‘中国’, ‘3’, ‘30’, ‘59’, ‘15’);
INSERT INTO commodity VALUES (‘43’, ‘php入门到精通’, ‘中国’, ‘3’, ‘30’, ‘59’, ‘25’);
INSERT INTO commodity VALUES (‘44’, ‘疯狂java’, ‘中国’, ‘3’, ‘72’, ‘166’, ‘12’);
INSERT INTO commodity VALUES (‘45’, ‘java思考1’, ‘美国’, ‘3’, ‘132’, ‘170’, ‘12’);
INSERT INTO commodity VALUES (‘46’, ‘成语典故’, ‘中国’, ‘3’, ‘13’, ‘26’, ‘101’);
INSERT INTO commodity VALUES (‘47’, ‘睡前小故事0-3’, ‘中国’, ‘3’, ‘20’, ‘40’, ‘20’);
INSERT INTO commodity VALUES (‘48’, ‘睡前小故事4-7’, ‘中国’, ‘3’, ‘20’, ‘40’, ‘20’);
INSERT INTO commodity VALUES (‘49’, ‘安徒生童话’, ‘中国’, ‘3’, ‘26’, ‘40’, ‘12’);
INSERT INTO commodity VALUES (‘50’, ‘格林童话’, ‘中国’, ‘3’, ‘23’, ‘29’, ‘12’);
INSERT INTO commodity VALUES (‘51’, ‘上下五千年’, ‘中国’, ‘3’, ‘200’, ‘400’, ‘3’);
INSERT INTO commodity VALUES (‘52’, ‘电脑报15年合集’, ‘中国’, ‘3’, ‘24’, ‘52’, ‘2’);
INSERT INTO commodity VALUES (‘53’, ‘哈利波特1-3’, ‘英国’, ‘3’, ‘120’, null, ‘3’);
INSERT INTO commodity VALUES (‘54’, ‘新华字典’, ‘中国’, ‘3’, ‘8’, ‘17’, ‘30’);
INSERT INTO commodity VALUES (‘55’, ‘英汉字典’, ‘中国’, ‘3’, ‘26’, ‘26’, ‘4’);
INSERT INTO commodity VALUES (‘56’, ‘牛津英语’, ‘英国’, ‘3’, ‘217’, ‘300’, ‘16’);
INSERT INTO commodity VALUES (‘57’, ‘唐诗三百首’, ‘中国’, ‘3’, ‘6’, ‘8’, ‘22’);
INSERT INTO commodity VALUES (‘58’, ‘名家演讲赏析’, ‘中国’, ‘3’, ‘22’, ‘50’, ‘4’);
INSERT INTO commodity VALUES (‘59’, ‘三国演义’, ‘中国’, ‘3’, ‘20’, ‘37’, ‘37’);
INSERT INTO commodity VALUES (‘60’, ‘红楼梦’, ‘中国’, ‘3’, ‘14’, ‘16’, ‘15’);

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值