mysql常用语句练习-基于ecshop2.7.3数据库

Posted ghostwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql常用语句练习-基于ecshop2.7.3数据库相关的知识,希望对你有一定的参考价值。

SELECT * FROM ecs_goods WHERE goods_id = 1;
SELECT goods_id, goods_name FROM ecs_goods WHERE goods_id = 1;
SELECT goods_id,cat_id,goods_name FROM ecs_goods WHERE cat_id != 3;
SELECT goods_id,cat_id,goods_name FROM ecs_goods WHERE cat_id <> 3;
SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE shop_price > 3000;
SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE shop_price <= 100;


SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE cat_id = 4 OR cat_id = 3;
SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE cat_id IN( 3, 4 );


SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE shop_price BETWEEN 100 AND 858;


SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE cat_id != 3 AND cat_id != 4
SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE cat_id NOT IN( 3, 4 );
SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE cat_id NOT BETWEEN 3 AND 4


SELECT goods_id,cat_id,goods_name,shop_price FROM ecs_goods WHERE ( shop_price > 100 AND shop_price < 300 )
OR ( shop_price > 1000 AND shop_price < 3000 );


SELECT goods_id,cat_id,goods_name,shop_price,click_count FROM ecs_goods WHERE (cat_id = 3 ) AND ( shop_price >= 1000 AND shop_price <= 3000 )
AND ( click_count > 5 )

 

SELECT * FROM ecs_goods WHERE cat_id IN ( 2, 3, 4, 5 )

SELECT goods_id,cat_id,goods_name FROM ecs_goods WHERE goods_name LIKE ‘诺基亚%‘

 

 #把表中字段num取值范围为20~29之间的值变成20【注:ecshop2.7.3没有这个ecs_mian1这个表,可以自己创建一个,他就一个字段num, int类型,然后填充一些测试数据】

UPDATE ecs_mian1 SET num = ( FLOOR( num / 10 ) * 10 ) WHERE num >= 20 AND num <= 29

 #把表中字段num取值范围为30~39之间的值变成30

UPDATE ecs_mian1 SET num = ( FLOOR( num / 10 ) * 10 ) WHERE num BETWEEN 30 AND 39

 

#以”诺基亚"开头的商品

SELECT goods_id, goods_name FROM ecs_goods WHERE goods_name LIKE ‘诺基亚%‘;

#截取"诺基亚"后面的商品名称,并用"小米"开始 连接起来
SELECT goods_id, CONCAT( ‘小米‘, SUBSTRING( goods_name, 4 ) ) FROM ecs_goods WHERE goods_name LIKE ‘诺基亚%‘;

#截取"诺基亚"后面的商品名称,并用"小米"开始 连接起来后更新
UPDATE ecs_goods SET goods_name = CONCAT( ‘小米‘, SUBSTRING( goods_name, 4 ) ) WHERE goods_name LIKE ‘诺基亚%‘

#以上面的语句相反
UPDATE ecs_goods SET goods_name = CONCAT( ‘诺基亚‘, SUBSTRING( goods_name, 4 ) ) WHERE goods_name LIKE ‘小米%‘

 

关于NULL:

SELECT NULL = NULL
SELECT NULL != NULL

#建表
CREATE TABLE ghost_user_info(
id INT( 3 ),
user_name VARCHAR( 20 )
)CHARSET utf8 ENGINE MYISAM;

#插入测试数据
INSERT INTO ghost_user_info VALUES( 1, ‘张三‘ );
INSERT INTO ghost_user_info VALUES( 2, NULL );

#user_name不等于NULL的记录,不能这样写
SELECT * FROM ghost_user_info WHERE user_name != NULL

#user_name不等于NULL的记录,应该这样写
SELECT * FROM ghost_user_info WHERE user_name IS NOT NULL

#同样的,等于NULL的记录应该是 IS NULL而不是 = NULL
SELECT * FROM ghost_user_info WHERE user_name = NULL
SELECT * FROM ghost_user_info WHERE user_name IS NULL

 #统计函数

SELECT AVG( shop_price ) FROM ecs_goods;
SELECT MAX( shop_price ) FROM ecs_goods;
SELECT MIN( shop_price ) FROM ecs_goods;
SELECT COUNT(*) FROM ecs_goods;
SELECT SUM( shop_price ) / COUNT(*) FROM ecs_goods;

#积压货款
SELECT SUM( shop_price * goods_number ) FROM ecs_goods;

 #统计每个分类的商品平均价格, group比较耗费资源(先按cat_id排序,再统计)

SELECT cat_id,AVG( shop_price ) FROM ecs_goods GROUP BY cat_id;

#查询每个分类下,商品的数量
SELECT cat_id, COUNT(*) FROM ecs_goods GROUP BY cat_id;

#每个分类下最贵的产品
SELECT cat_id, MAX(shop_price) FROM ecs_goods GROUP BY cat_id;

 

#本店价格比市场价格小200以上的商品
SELECT goods_id, goods_name, market_price - shop_price FROM ecs_goods WHERE market_price - shop_price > 200

#下面的语句,报错(Unknown column ‘discount_price‘ in ‘where clause‘ ),discount_price这个别名是结果集中(存在内存中)的,where后面的字段需要是磁盘表中的字段

SELECT goods_id, goods_name, ( market_price - shop_price ) AS discount_price FROM ecs_goods WHERE discount_price > 200

#可以用having关键字过滤查询出来的结果集
SELECT goods_id, goods_name, ( market_price - shop_price ) AS discount_price FROM ecs_goods WHERE 1 HAVING discount_price > 200

 

以上是关于mysql常用语句练习-基于ecshop2.7.3数据库的主要内容,如果未能解决你的问题,请参考以下文章

MySQL试题 --- 31道巩固 SQL 语句的练习题

入门MySQL——查询语法练习

零基础学MySQL-- 数据库最常用的操作查询基础篇 -- 单表查询

零基础学MySQL-- 数据库最常用的操作查询基础篇 -- 单表查询

#yyds干货盘点# MySQL数据库语句进阶--随手笔记

超详细的 MySQL 学习教程二(多实例附练习视频讲解持续更新)