爱易网
IT新闻
IT新闻
爱易资讯
网站搭建
云虚拟主机教程
云服务器教程
Apache教程
IIS教程
Nginx教程
网站策划
站长文章
推广教程
淘宝客教程
网页设计
HTML教程
XHTML教程
CSS教程
HTML5教程
CSS3教程
JavaSript基础
JQuery教程
Node.js教程
前端技术
Ajax教程
Js特效
Xml教程
平面设计
页面UI设计
photoshop教程
程序开发
AI人工智能
Asp教程
Php教程
Asp.Net教程
Net Core教程
C#教程
Java教程
Jsp教程
开发技术
微信小程序教程
Uniapp开发教程
微信公众号开发
Andriod教程
IOS教程
DOS教程
Python教程
Docker教程
Windows Container教程
数据库
MSSQL教程
MySQL教程
Redis教程
Access教程
Oracle教程
数据库教程
操作系统
Linux教程
Windows教程
MAC教程
Cisco教程
交换机教程
防火墙教程
搜索
爱易网页
MySQL教程
刘说成 mysql 学习笔记2
刘说成 mysql 学习笔记2
日期:2014-05-16 浏览次数:21070 次
刘道成 mysql 学习笔记2
仿ecshop建库 create database mugua charset utf8; use mugua; create table goods( goods_id int primary key auto_increment, cat_id smallint not null default 0, goods_sn char(15) not null default '', goods_name varchar(30) not null default '', click_count mediumint unsigned not null default 0, brand_id smallint not null default 0, goods_number smallint not null default 0, marcket_price decimal(7,2) not null default 0.00, shop_price decimal(7,2) not null default 0.00, add_time int unsigned not null default 0 )charset utf8; 从一个数据库导数据到另一个数据库的方法 会碰到中文出现乱码 先运行这个语句 set names gbk insert into mugua.goods select goods_id,cat_id,goods_sn,goods_name,click_count, brand_id,goods_number,market_price,shop_price,add_time from shop.goods; create table category( cat_id smallint primary key auto_increment, cat_name varchar(30) not null default '', parent_id smallint not null default 0 )charset utf8; insert into mugua.category select cat_id,cat_name,parent_id from shop.category; create table brand( brand_id smallint primary key auto_increment, brand_name varchar(30) not null default'' )charset utf8; insert into mugua.brand select brand_id,brand_name from shop.brand; (一)where 表达式在哪一(几)行成立 就把那一(几)行取出来 select * from goods where cat_id=3; select * from goods where cat_id !=3; 等价于 select * from goods where cat_id <>3; select * from goods where cat_id>3; select * from goods where cat_id<3; select * from goods where cat_id>=3; select * from goods where cat_id<=3; select * from goods where cat_id in(3,4); select * from goods where cat_id between 3 and 5; select * from goods where cat_id >3 or cat_id <=2; select * from goods where cat_id>=3 and cat_id<=6; (二)group by 分组查询 group by 与max min sum avg count连用 1 查出最贵商品价格 select max(shop_price) from goods; 2 查出最便宜商品价格 select min(shop_price) from goods; 3 查出商品总量 select sum(goods_number) from goods; 4 查出所有商品平均价格 select avg(shop_price) from goods; 5,查出所有商品总类 select count(goods_id) from goods; #查询每个栏目下 要用到group by 1 查出最贵商品价格 select cat_id,max(shop_price) from goods group by cat_id; 2 查出最便宜商品价格 select cat_id,min(shop_price) from goods group by cat_id; 3 查出商品总量 select cat_id,sum(goods_number) from goods group by cat_id; 4 查出所有商品平均价格 select cat_id,avg(shop_price) from goods group by cat_id; 5,查出所有商品总类 select cat_id,count(goods_id) from goods group by cat_id; #把列名当变量就可以进行运算了 可以写出更加高效的sql语句 #查询每个栏目下积压的货款 select cat_id,sum(shop_price*goods_number) from goods group by cat_id; #查询出每个商品比市场价低多少 select goods_id,goods_name,marcket_price-shop_price as cha from goods; (三)having查询 where 发挥作用的时间在查表结果之前 having 是筛选 是对查询结果的进一步选择 1,查询比市场价省200元以上得物品价格以及所省的钱 select goods_name,goods_id,marcket_price-shop_price as sheng from goods having sheng>200; 如果写成 select goods_name,goods_id,marcket_price-shop_price as sheng from goods where sheng>200; 是错误的 当然也可以写成 select goods_name,goods_id,marcket_price-shop_price as sheng from goods where marcket_price-shop_price>200; 只不错计算了两次 不如having 好 2,查询积压货款超过 20000元货款的栏目 以及该货款 select cat_id,sum(shop_price*goods_number) as jiya from goods group by cat_id having jiya>20000; 更多资源 http://www.taobtbw.com/ (四)order by 和limit查询 1,按价格由高到低排序 select goods_name,goods_id,shop_price from goods order by shop_price desc; 2,按发布时间由早到晚排序 select goods_id,goods_name,add_time from goods order by add_time; 3,按栏目由低到高排序,栏目内部按价格由高到低排序 select goods_id,goods_name,cat_id,shop_price from goods order by cat_id asc,shop_price desc; limit 的用法 limit在语句的最后 起到限制的作用 limit[offset]N offset :偏移量 N;去处的条目数量 offset不写则相当于 limit 0,N 是从0 开始算的 而不是1 4,取出价格最高的前三名商品 select goods_id,goods_name,shop_price from goods order by shop_price desc limit 0,3; 5,取出点击量前三名到前5名的商品 select goods_id,goods_name,click_count from goods order by click_count desc limit 2,3; 思考题 :取出每个栏目下最贵的商品 错误1:select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id 错误原因是用了聚合函数 而其他栏目以第一次遇到的为准 错误2: selec
上一篇:MYSQL锁处理
下一篇: java联接mysql示例代码
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
绿色版mysql装配使用
mysql主从表与读写分开
分页(mysql,其余数据可使用类似方式)
MySql存储过程—一、SQL存储过程的基础知识
Ofbiz12.04 进阶之一 怎么使ofbiz连接Mysql
菜鸟应该用哪个版本的MY-SQL啊
怎么检索一个月内连续登入的用户
有什么办法,可以在WINXP上把LINUX里的MYSQL数据库完全备份到WINXP机器?或者是导出到WINXP机器?该怎么解决
请问一个MYSQL两表关联的效率有关问题。(每张表2000W条数据)
推荐阅读
更多>
怎么实时查看mysql当前连接数
请教:小弟我在postgres里面建了一个表后,小弟我能不能将建表的语句拷出来啊这样以后建表时就可以用这个语句就可以了
Ubuntu停Can't connect to local MySQL server through socket
线下从库服务器临时表报错如上,有碰到么,share上解决办法
請問mysql cursor可以用變數開啟嗎?解决方案
旁人写的一个mysql数据处理的javabean
经典SQL语句解决思路
Mysql应用自定义方法,以及cakephp分页使用join查询的方法
mysql binlog清算
mysql命令行的一些小技能
Oracle、Mysql、SqlServer中安插多条数据
Ubuntu停的sqldeveloper增加MySQL连接
mysql 增添字段、删除字段、调整字段顺序 转
mysql语句查询,求大牛指导,该如何解决
MYSQL中的日期变换
关于MYSQL排序有关问题
MySql优化之:抉择优化的数据类型
怎么记录mysql慢查询sql日志
如用一条话语 将10003到10005之间的valid改成0
mysql不能连接本机服务器解决办法