MySQL SQL 查询相关 发表于 2019-12-23 | 分类于 MySQL MySql COUNT 加 IF 条件 123#以下两条SQL查询结果一致SELECT COUNT(1) FROM user WHERE account_status = 0;SELECT COUNT(IF(account_status = 0, true, null)) FROM user; MySql 排名、并列排名、查询指定用户名次 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263-- 用户表CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(16) NOT NULL, `score` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;-- 测试数据INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (1, 'A', 7);INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (2, 'B', 3);INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (3, 'C', 4);INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (4, 'D', 0);INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (5, 'E', 4);INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (6, 'F', 9);INSERT INTO `test`.`user`(`id`, `name`, `score`) VALUES (7, 'G', 2);-- 说明@rank := @rank + 1 中, := 是赋值的作用, 意思是先执行 @rank + 1, 然后把值赋给 @rank(SELECT @rank := 0) r 意思是设置 rank 字段的初始值为0-- 1. 查询结果后排序SELECT u.*, @rank := @rank + 1 AS rankFROM (SELECT @rank := 0) r, `user` uORDER BY u.score DESC;-- 2. 并列排名SELECT u.*, (CASE WHEN @prev = u.score THEN @rank WHEN @prev := u.score THEN @rank := @rank + 1 WHEN @prev = 0 THEN @rank := @rank + 1 END) AS rankFROM (SELECT @rank := 0, @prev := NULL) r, `user` uORDER BY u.score DESC;-- 3. 指定用户排名SELECT *FROM ( SELECT u.*, (CASE WHEN @prev = u.score THEN @rank WHEN @prev := u.score THEN @rank := @rank + 1 WHEN @prev = 0 THEN @rank := @rank + 1 END) AS rank FROM (SELECT @rank := 0, @prev := NULL) r, `user` u ORDER BY u.score DESC ) urWHERE ur.id = 3; MySql 查询每件商品最低价格 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748# 1. 创建表CREATE TABLE `goods_sku` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'SKU ID', `goods_id` int(11) unsigned NOT NULL COMMENT '商品ID', `sku_title` varchar(255) NOT NULL COMMENT '集成名称', `sku_price` decimal(12,2) NOT NULL COMMENT '价格', `sku_stock` int(11) NOT NULL COMMENT '库存', PRIMARY KEY (`id`) USING BTREE, KEY `goods_id` (`goods_id`) USING BTREE, KEY `goods_id_price` (`goods_id`,`sku_price`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品SKU信息表';# 2. 插入测试数据INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (1, 1, '颜色:红色 尺寸:l', 0.02, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (2, 1, '颜色:红色 尺寸:xl', 0.01, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (3, 1, '颜色:黄色 尺寸:l', 0.03, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (4, 1, '颜色:黄色 尺寸:xl', 0.04, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (5, 2, '颜色:红色 尺寸:l', 0.02, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (6, 2, '颜色:红色 尺寸:xl', 0.02, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (7, 2, '颜色:黄色 尺寸:l', 0.07, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (8, 2, '颜色:黄色 尺寸:xl', 0.01, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (9, 3, '颜色:红色 尺寸:l', 0.01, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (10, 3, '颜色:红色 尺寸:xl', 0.02, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (11, 3, '颜色:黄色 尺寸:l', 0.03, 10);INSERT INTO `goods_sku`(`id`, `goods_id`, `sku_title`, `sku_price`, `sku_stock`) VALUES (12, 3, '颜色:黄色 尺寸:xl', 0.04, 10);# 3.1 查询语句 - 子查询SELECT id, sku_price FROM goods_sku gs WHERE NOT EXISTS ( SELECT 1 FROM goods_sku gss WHERE gs.goods_id = gss.goods_id AND gs.sku_price > gss.sku_price )GROUP BY goods_id; # 3.2 查询语句 - JOIN SELECT gs.id, gs.sku_price #,gss.id #,gss.sku_priceFROM goods_sku gs LEFT JOIN goods_sku gss ON gss.goods_id = gs.goods_id AND gss.sku_price < gs.sku_price WHERE gss.id IS NULL 本文作者:Mr 本文链接: http://sevming.github.io/MySQL/mysql-sql.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!