1. mysql复习
  2. 一:复习前的准备
  3. 1:确认你已安装wamp
  4. 2:确认你已安装ecshop,并且ecshop的数据库名为shop
  5.  
  6. 基础知识:
  7. 1.数据库的连接
  8. mysql -u -p -h
  9. -u 用户名
  10. -p 密码
  11. -h host主机
  12.  
  13. 2:库级知识
  14. 2.1 显示数据库: show databases;
  15. 2.2 选择数据库: use dbname;
  16. 2.3 创建数据库: create database dbname charset utf8;
  17. 2.3 删除数据库: drop database dbname;
  18.  
  19. 3: 表级操作:
  20. 3.1 显示库下面的表
  21. show tables;
  22.  
  23. 3.2 查看表的结构:
  24. desc tableName;
  25.  
  26. 3.3 查看表的创建过程:
  27. show create table tableName;
  28.  
  29. 3.4 创建表:
  30.  create table tbName (
  31. 列名称1 列类型 [列参数] [not null default ],
  32. ....列2...
  33. ....
  34. 列名称N 列类型 [列参数] [not null default ]
  35. )engine myisam/innodb charset utf8/gbk
  36.  
  37. 3.4的例子:
  38. create table user (
  39. id int auto_increment,
  40. name varchar(20) not null default '',
  41. age tinyint unsigned not null default 0,
  42. index id (id)
  43. )engine=innodb charset=utf8;
  44. 注:innodb是表引擎,也可以是myisam或其他,但最常用的是myisaminnodb,
  45. charset 常用的有utf8,gbk;
  46.  
  47. 3.5 修改表
  48. 3.5.1 修改表之增加列:
  49. alter table tbName
  50. add 列名称1 列类型 [列参数] [not null default ] #(add之后的旧列名之后的语法和创建表时的列声明一样)
  51.  
  52. 3.5.2 修改表之修改列
  53. alter table tbName
  54. change 旧列名 新列名 列类型 [列参数] [not null default ]
  55. (注:旧列名之后的语法和创建表时的列声明一样)
  56.  
  57. 3.5.3 修改表之减少列:
  58. alter table tbName
  59. drop 列名称;
  60.  
  61. 3.5.4 修改表之增加主键
  62. alter table tbName add primary key(主键所在列名);
  63. 例:alter table goods add primary key(id)
  64. 该例是把主键建立在id列上
  65.  
  66. 3.5.5 修改表之删除主键
  67. alter table tbName drop primary key;
  68.  
  69. 3.5.6 修改表之增加索引
  70. alter table tbName add [unique|fulltext] index 索引名(列名);
  71.  
  72. 3.5.7 修改表之删除索引
  73. alter table tbName drop index 索引名;
  74.  
  75. 3.5.8 清空表的数据
  76. truncate tableName;
  77.  
  78. 4:列类型讲解
  79. 列类型:
  80. 整型:tinyint (0~255/-128~127) smallint (0~65535/-32768~32767) mediumint int bigint (参考手册11.2)
  81. 参数解释:
  82. unsigned 无符号(不能为负) zerofill 0填充 M 填充后的宽度
  83. 举例:tinyint unsigned;
  84. tinyint(6) zerofill;
  85. 数值型
  86. 浮点型:float double
  87. 格式:float(M,D) unsigned\zerofill;
  88.  
  89. 字符型
  90. char(m) 定长
  91. varchar(m)变长
  92. text
  93.  
  94. 实存字符i 实占空间 利用率
  95.  
  96. char(M) 0<=i<=M M i/m<=100%
  97.  
  98. varchar(M) 0<=i<=M i+1,2 i/i+1/2<100%
  99.  
  100. year YYYY 范围:1901~2155. 可输入值2位和4位(如98,2012)
  101. 日期时间类型 date YYYY-MM-DD 如:2010-03-14
  102. time HH:MM:SS 如:19:26:32
  103. datetime YYYY-MM-DD HH:MM:SS 如:2010-03-14 19:26:32
  104. timestamp YYYY-MM-DD HH:MM:SS 特性:不用赋值,该列会为自己赋当前的具体时间
  105.  
  106. 5:增删改查基本操作
  107.  
  108. 5.1 插入数据
  109. insert into 表名(col1,col2,……) values(val1,val2……); -- 插入指定列
  110. insert into 表名 values (,,,,); -- 插入所有列
  111. insert into 表名 values -- 一次插入多行
  112. (val1,val2……),
  113. (val1,val2……),
  114. (val1,val2……);
  115.  
  116. 5.3修改数据
  117. update tablename
  118. set
  119. col1=newval1,
  120. col2=newval2,
  121. ...
  122. ...
  123. colN=newvalN
  124. where 条件;
  125.  
  126. 5.4,删除数据 delete from tablenaeme where 条件;
  127.  
  128. 5.5 select 查询
  129.  
  130. 1 条件查询 where a. 条件表达式的意义,表达式为真,则该行取出
  131. b. 比较运算符 = ,!=,< > <= >=
  132. c. like , not like ('%'匹配任意多个字符,'_'匹配任意单个字符)
  133. in , not in , between and
  134. d. is null , is not null
  135. 2 分组 group by
  136. 一般要配合5个聚合函数使用:max,min,sum,avg,count
  137. 3 筛选 having
  138. 4 排序 order by
  139. 5 限制 limit
  140.  
  141. 6: 连接查询
  142.  
  143. 6.1 左连接
  144. .. left join .. on
  145. table A left join table B on tableA.col1 = tableB.col2 ;
  146. 例句:
  147. select 列名 from table A left join table B on tableA.col1 = tableB.col2
  148. 2. 右链接: right join
  149. 3. 内连接: inner join
  150.  
  151. 左右连接都是以在左边的表的数据为准,沿着左表查右表.
  152. 内连接是以两张表都有的共同部分数据为准,也就是左右连接的数据之交集.
  153.  
  154. 7 子查询
  155. where 型子查询:内层sql的返回值在where后作为条件表达式的一部分
  156. 例句: select * from tableA where colA = (select colB from tableB where ...);
  157.  
  158. from 型子查询:内层sql查询结果,作为一张表,供外层的sql语句再次查询
  159. 例句:select * from (select * from ...) as tableName where ....
  160.  
  161. 8: 字符集
  162. 客服端sql编码 character_set_client
  163. 服务器转化后的sql编码 character_set_connection
  164. 服务器返回给客户端的结果集编码 character_set_results
  165. 快速把以上3个变量设为相同值: set names 字符集
  166.  
  167. 存储引擎 engine=1\2
  168. 1 Myisam 速度快 不支持事务 回滚
  169. 2 Innodb 速度慢 支持事务,回滚
  170.  
  171. ①开启事务 start transaction
  172. ②运行sql;
  173. ③提交,同时生效\回 commit\rollback
  174.  
  175. 触发器 trigger
  176. 监视地点:表
  177. 监视行为:增
  178. 触发时间:after\before
  179. 触发事件:增
  180.  
  181. 创建触发器语法
  182. create trigger tgName
  183. after/before insert/delete/update
  184. on tableName
  185. for each row
  186. sql; -- 触发语句
  187.  
  188. 删除触发器:drop trigger tgName;
  189.  
  190. 索引
  191. 提高查询速度,但是降低了增删改的速度,所以使用索引时,要综合考虑.
  192. 索引不是越多越好,一般我们在常出现于条件表达式中的列加索引.
  193. 值越分散的列,索引的效果越好
  194.  
  195. 索引类型
  196. primary key主键索引
  197. index 普通索引
  198. unique index 唯一性索引
  199. fulltext index 全文索引
  200.  
  201. 综合练习:
  202. 连接上数据库服务器
  203. 创建一个gbk编码的数据库
  204. 建立商品表和栏目表,字段如下:
  205.  
  206. 商品表:goods
  207. goods_id --主键,
  208. goods_name -- 商品名称
  209. cat_id -- 栏目id
  210. brand_id -- 品牌id
  211. goods_sn -- 货号
  212. goods_number -- 库存量
  213. shop_price -- 价格
  214. goods_desc --商品详细描述
  215.  
  216. 栏目表:category
  217. cat_id --主键
  218. cat_name -- 栏目名称
  219. parent_id -- 栏目的父id
  220.  
  221. 建表完成后,作以下操作:
  222. 删除goods表的goods_desc 字段,及货号字段
  223. 并增加字段:click_count -- 点击量
  224.  
  225. goods_name列上加唯一性索引
  226. shop_price列上加普通索引
  227. clcik_count列上加普通索引
  228. 删除click_count列上的索引
  229.  
  230. goods表插入以下数据:
  231. +----------+------------------------------+--------+----------+-----------+--------------+------------+-------------+
  232. | goods_id | goods_name | cat_id | brand_id | goods_sn | goods_number | shop_price | click_count |
  233. +----------+------------------------------+--------+----------+-----------+--------------+------------+-------------+
  234. | 1 | KD876 | 4 | 8 | ECS000000 | 10 | 1388.00 | 7 |
  235. | 4 | 诺基亚N85原装充电器 | 8 | 1 | ECS000004 | 17 | 58.00 | 0 |
  236. | 3 | 诺基亚原装5800耳机 | 8 | 1 | ECS000002 | 24 | 68.00 | 3 |
  237. | 5 | 索爱原装M2卡读卡器 | 11 | 7 | ECS000005 | 8 | 20.00 | 3 |
  238. | 6 | 胜创KINGMAX内存卡 | 11 | 0 | ECS000006 | 15 | 42.00 | 0 |
  239. | 7 | 诺基亚N85原装立体声耳机HS-82 | 8 | 1 | ECS000007 | 20 | 100.00 | 0 |
  240. | 8 | 飞利浦9@9v | 3 | 4 | ECS000008 | 17 | 399.00 | 9 |
  241. | 9 | 诺基亚E66 | 3 | 1 | ECS000009 | 13 | 2298.00 | 20 |
  242. | 10 | 索爱C702c | 3 | 7 | ECS000010 | 7 | 1328.00 | 11 |
  243. | 11 | 索爱C702c | 3 | 7 | ECS000011 | 1 | 1300.00 | 0 |
  244. | 12 | 摩托罗拉A810 | 3 | 2 | ECS000012 | 8 | 983.00 | 14 |
  245. | 13 | 诺基亚5320 XpressMusic | 3 | 1 | ECS000013 | 8 | 1311.00 | 13 |
  246. | 14 | 诺基亚5800XM | 4 | 1 | ECS000014 | 4 | 2625.00 | 6 |
  247. | 15 | 摩托罗拉A810 | 3 | 2 | ECS000015 | 3 | 788.00 | 8 |
  248. | 16 | 恒基伟业G101 | 2 | 11 | ECS000016 | 0 | 823.33 | 3 |
  249. | 17 | 夏新N7 | 3 | 5 | ECS000017 | 1 | 2300.00 | 2 |
  250. | 18 | 夏新T5 | 4 | 5 | ECS000018 | 1 | 2878.00 | 0 |
  251. | 19 | 三星SGH-F258 | 3 | 6 | ECS000019 | 0 | 858.00 | 7 |
  252. | 20 | 三星BC01 | 3 | 6 | ECS000020 | 13 | 280.00 | 14 |
  253. | 21 | 金立 A30 | 3 | 10 | ECS000021 | 40 | 2000.00 | 4 |
  254. | 22 | 多普达Touch HD | 3 | 3 | ECS000022 | 0 | 5999.00 | 15 |
  255. | 23 | 诺基亚N96 | 5 | 1 | ECS000023 | 8 | 3700.00 | 17 |
  256. | 24 | P806 | 3 | 9 | ECS000024 | 148 | 2000.00 | 36 |
  257. | 25 | 小灵通/固话50元充值卡 | 13 | 0 | ECS000025 | 2 | 48.00 | 0 |
  258. | 26 | 小灵通/固话20元充值卡 | 13 | 0 | ECS000026 | 2 | 19.00 | 0 |
  259. | 27 | 联通100元充值卡 | 15 | 0 | ECS000027 | 2 | 95.00 | 0 |
  260. | 28 | 联通50元充值卡 | 15 | 0 | ECS000028 | 0 | 45.00 | 0 |
  261. | 29 | 移动100元充值卡 | 14 | 0 | ECS000029 | 0 | 90.00 | 0 |
  262. | 30 | 移动20元充值卡 | 14 | 0 | ECS000030 | 9 | 18.00 | 1 |
  263. | 31 | 摩托罗拉E8 | 3 | 2 | ECS000031 | 1 | 1337.00 | 5 |
  264. | 32 | 诺基亚N85 | 3 | 1 | ECS000032 | 1 | 3010.00 | 9 |
  265. +----------+------------------------------+--------+----------+-----------+--------------+------------+-------------+
  266.  
  267. 查询知识
  268. 注:以下查询基于ecshop网站的商品表(ecs_goods)
  269. 在练习时可以只取部分列,方便查看.
  270.  
  271. 1: 基础查询 where的练习:
  272.  
  273. 查出满足以下条件的商品
  274. 1.1:主键为32的商品
  275. select goods_id,goods_name,shop_price
  276. from ecs_goods
  277. where goods_id=32;
  278. 1.2:不属第3栏目的所有商品
  279. select goods_id,cat_id,goods_name,shop_price from ecs_goods
  280. where cat_id!=3;
  281.  
  282. 1.3:本店价格高于3000元的商品
  283.  
  284. select goods_id,cat_id,goods_name,shop_price from ecs_goods
  285. where shop_price >3000;
  286.  
  287. 1.4:本店价格低于或等于100元的商品
  288. select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price <=100;
  289.  
  290. 1.5:取出第4栏目或第11栏目的商品(不许用or)
  291. select goods_id,cat_id,goods_name,shop_price from ecs_goods
  292. where cat_id in (4,11);
  293.  
  294. 1.6:取出100<=价格<=500的商品(不许用and)
  295. select goods_id,cat_id,goods_name,shop_price from ecs_goods
  296. where shop_price between 100 and 500;
  297.  
  298. 1.7:取出不属于第3栏目且不属于第11栏目的商品(and,或not in分别实现)
  299. select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id!=3 and cat_id!=11;
  300.  
  301. select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id not in (3,11);
  302.  
  303. 1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品()
  304. select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price>100 and shop_price4000 and shop_price <5000;
  305.  
  306. 1.9:取出第3个栏目下面价格<1000或>3000,并且点击量>5的系列商品
  307. select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods where
  308. cat_id=3 and (shop_price3000) and click_count>5;
  309.  
  310. 1.10:取出第1个栏目下面的商品(注意:1栏目下面没商品,但其子栏目下有)
  311. select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods
  312. where cat_id in (2,3,4,5);
  313.  
  314. 1.11:取出名字以"诺基亚"开头的商品
  315. select goods_id,cat_id,goods_name,shop_price from ecs_goods where goods_name like '诺基亚%';
  316.  
  317. 1.12:取出名字为"诺基亚Nxx"的手机
  318. select goods_id,cat_id,goods_name,shop_price from ecs_goods
  319. where goods_name like '诺基亚N__';
  320.  
  321. 1.13:取出名字不以"诺基亚"开头的商品
  322. select goods_id,cat_id,goods_name,shop_price from ecs_goos
  323. where goods_name not like '诺基亚%';
  324.  
  325. 1.14:取出第3个栏目下面价格在10003000之间,并且点击量>5 "诺基亚"开头的系列商品
  326. select goods_id,cat_id,goods_name,shop_price from ecs_goods where
  327. cat_id=3 and shop_price>1000 and shop_price5 and goods_name like '诺基亚%';
  328.  
  329. select goods_id,cat_id,goods_name,shop_price from ecs_goods where
  330. shop_price between 1000 and 3000 and cat_id=3 and click_count>5 and goods_name like '诺基亚%';
  331.  
  332. 一道面试题
  333. 有如下表和数组
  334. num值处于[20,29]之间,改为20
  335. num值处于[30,39]之间的,改为30
  336.  
  337. mian
  338. +------+
  339. | num |
  340. +------+
  341. | 3 |
  342. | 12 |
  343. | 15 |
  344. | 25 |
  345. | 23 |
  346. | 29 |
  347. | 34 |
  348. | 37 |
  349. | 32 |
  350. | 45 |
  351. | 48 |
  352. | 52 |
  353. +------+
  354.  
  355. 练习题:
  356. good表中商品名为'诺基亚xxxx'的商品,改为'HTCxxxx',
  357. 提示:大胆的把列看成变量,参与运算,甚至调用函数来处理 .
  358. substring(),concat()
  359.  
  360. 2 分组查询group:
  361. 2.1:查出最贵的商品的价格
  362. select max(shop_price) from ecs_goods;
  363.  
  364. 2.2:查出最大(最新)的商品编号
  365. select max(goods_id) from ecs_goods;
  366.  
  367. 2.3:查出最便宜的商品的价格
  368. select min(shop_price) from ecs_goods;
  369.  
  370. 2.4:查出最旧(最小)的商品编号
  371. select min(goods_id) from ecs_goods;
  372.  
  373. 2.5:查询该店所有商品的库存总量
  374. select sum(goods_number) from ecs_goods;
  375.  
  376. 2.6:查询所有商品的平均价
  377. select avg(shop_price) from ecs_goods;
  378.  
  379. 2.7:查询该店一共有多少种商品
  380. select count(*) from ecs_goods;
  381.  
  382. 2.8:查询每个栏目下面
  383. 最贵商品价格
  384. 最低商品价格
  385. 商品平均价格
  386. 商品库存量
  387. 商品种类
  388. 提示:(5个聚合函数,sum,avg,max,min,countgroup综合运用)
  389. select cat_id,max(shop_price) from ecs_goods group by cat_id;
  390.  
  391. 3 havinggroup综合运用查询:
  392. 3.1:查询该店的商品比市场价所节省的价格
  393. select goods_id,goods_name,market_price-shop_price as j
  394. from ecs_goods ;
  395.  
  396. 3.2:查询每个商品所积压的货款(提示:库存*单价)
  397. select goods_id,goods_name,goods_number*shop_price from ecs_goods
  398.  
  399. 3.3:查询该店积压的总货款
  400. select sum(goods_number*shop_price) from ecs_goods;
  401.  
  402. 3.4:查询该店每个栏目下面积压的货款.
  403. select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id;
  404.  
  405. 3.5:查询比市场价省钱200元以上的商品及该商品所省的钱(wherehaving分别实现)
  406. select goods_id,goods_name,market_price-shop_price as k from ecs_goods
  407. where market_price-shop_price >200;
  408.  
  409. select goods_id,goods_name,market_price-shop_price as k from ecs_goods
  410. having k >200;
  411.  
  412. 3.6:查询积压货款超过2W元的栏目,以及该栏目积压的货款
  413. select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id
  414. having k>20000
  415.  
  416. 3.7:where-having-group综合练习题
  417. 有如下表及数据
  418. +------+---------+-------+
  419. | name | subject | score |
  420. +------+---------+-------+
  421. | 张三 | 数学 | 90 |
  422. | 张三 | 语文 | 50 |
  423. | 张三 | 地理 | 40 |
  424. | 李四 | 语文 | 55 |
  425. | 李四 | 政治 | 45 |
  426. | 王五 | 政治 | 30 |
  427. +------+---------+-------+
  428.  
  429. 要求:查询出2门及2门以上不及格者的平均成绩
  430.  
  431. ## 一种错误做法
  432. mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
  433. +------+---+------------+
  434. | name | k | avg(score) |
  435. +------+---+------------+
  436. | 张三 | 3 | 60.0000 |
  437. | 李四 | 2 | 50.0000 |
  438. +------+---+------------+
  439. 2 rows in set (0.00 sec)
  440.  
  441. mysql> select name,count(score<60) as k,avg(score) from stu group by name;
  442. +------+---+------------+
  443. | name | k | avg(score) |
  444. +------+---+------------+
  445. | 张三 | 3 | 60.0000 |
  446. | 李四 | 2 | 50.0000 |
  447. | 王五 | 1 | 30.0000 |
  448. +------+---+------------+
  449. 3 rows in set (0.00 sec)
  450.  
  451. mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
  452. +------+---+------------+
  453. | name | k | avg(score) |
  454. +------+---+------------+
  455. | 张三 | 3 | 60.0000 |
  456. | 李四 | 2 | 50.0000 |
  457. +------+---+------------+
  458. 2 rows in set (0.00 sec)
  459.  
  460. #加上赵六后错误暴露
  461. mysql> insert into stu
  462. -> values
  463. -> ('赵六','A',100),
  464. -> ('赵六','B',99),
  465. -> ('赵六','C',98);
  466. Query OK, 3 rows affected (0.05 sec)
  467. Records: 3 Duplicates: 0 Warnings: 0
  468.  
  469. #错误显现
  470. mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
  471. +------+---+------------+
  472. | name | k | avg(score) |
  473. +------+---+------------+
  474. | 张三 | 3 | 60.0000 |
  475. | 李四 | 2 | 50.0000 |
  476. | 赵六 | 3 | 99.0000 |
  477. +------+---+------------+
  478. 3 rows in set (0.00 sec)
  479.  
  480. #正确思路,先查看每个人的平均成绩
  481. mysql> select name,avg(score) from stu group by name;
  482. +------+------------+
  483. | name | avg(score) |
  484. +------+------------+
  485. | 张三 | 60.0000 |
  486. | 李四 | 50.0000 |
  487. | 王五 | 30.0000 |
  488. | 赵六 | 99.0000 |
  489. +------+------------+
  490. 4 rows in set (0.00 sec)
  491.  
  492. mysql> # 看每个人挂科情况
  493. mysql> select name,score < 60 from stu;
  494. +------+------------+
  495. | name | score < 60 |
  496. +------+------------+
  497. | 张三 | 0 |
  498. | 张三 | 1 |
  499. | 张三 | 1 |
  500. | 李四 | 1 |
  501. | 李四 | 1 |
  502. | 王五 | 1 |
  503. | 赵六 | 0 |
  504. | 赵六 | 0 |
  505. | 赵六 | 0 |
  506. +------+------------+
  507. 9 rows in set (0.00 sec)
  508.  
  509. mysql> #计算每个人的挂科科目
  510. mysql> select name,sum(score < 60) from stu group by name;
  511. +------+-----------------+
  512. | name | sum(score < 60) |
  513. +------+-----------------+
  514. | 张三 | 2 |
  515. | 李四 | 2 |
  516. | 王五 | 1 |
  517. | 赵六 | 0 |
  518. +------+-----------------+
  519. 4 rows in set (0.00 sec)
  520.  
  521. #同时计算每人的平均分
  522. mysql> select name,sum(score < 60),avg(score) as pj from stu group by name;
  523. +------+-----------------+---------+
  524. | name | sum(score < 60) | pj |
  525. +------+-----------------+---------+
  526. | 张三 | 2 | 60.0000 |
  527. | 李四 | 2 | 50.0000 |
  528. | 王五 | 1 | 30.0000 |
  529. | 赵六 | 0 | 99.0000 |
  530. +------+-----------------+---------+
  531. 4 rows in set (0.00 sec)
  532.  
  533. #利用having筛选挂科2门以上的.
  534. mysql> select name,sum(score < 60) as gk ,avg(score) as pj from stu group by name having gk >=2;
  535. +------+------+---------+
  536. | name | gk | pj |
  537. +------+------+---------+
  538. | 张三 | 2 | 60.0000 |
  539. | 李四 | 2 | 50.0000 |
  540. +------+------+---------+
  541. 2 rows in set (0.00 sec)
  542.  
  543. 4: order by limit查询
  544. 4.1:按价格由高到低排序
  545. select goods_id,goods_name,shop_price from ecs_goods order by shop_price desc;
  546.  
  547. 4.2:按发布时间由早到晚排序
  548. select goods_id,goods_name,add_time from ecs_goods order by add_time;
  549.  
  550. 4.3:接栏目由低到高排序,栏目内部按价格由高到低排序
  551. select goods_id,cat_id,goods_name,shop_price from ecs_goods
  552. order by cat_id ,shop_price desc;
  553.  
  554. 4.4:取出价格最高的前三名商品
  555. select goods_id,goods_name,shop_price from ecs_goods order by shop_price desc limit 3;
  556.  
  557. 4.5:取出点击量前三名到前5名的商品
  558. select goods_id,goods_name,click_count from ecs_goods order by click_count desc limit 2,3;
  559.  
  560. 5 连接查询
  561. 5.1:取出所有商品的商品名,栏目名,价格
  562. select goods_name,cat_name,shop_price from
  563. ecs_goods left join ecs_category
  564. on ecs_goods.cat_id=ecs_category.cat_id;
  565.  
  566. 5.2:取出第4个栏目下的商品的商品名,栏目名,价格
  567. select goods_name,cat_name,shop_price from
  568. ecs_goods left join ecs_category
  569. on ecs_goods.cat_id=ecs_category.cat_id
  570. where ecs_goods.cat_id = 4;
  571.  
  572. 5.3:取出第4个栏目下的商品的商品名,栏目名,与品牌名
  573. select goods_name,cat_name,brand_name from
  574. ecs_goods left join ecs_category
  575. on ecs_goods.cat_id=ecs_category.cat_id
  576. left join ecs_brand
  577. on ecs_goods.brand_id=ecs_brand.brand_id
  578. where ecs_goods.cat_id = 4;
  579.  
  580. 5.4: 用友面试题
  581.  
  582. 根据给出的表结构按要求写出SQL语句。
  583. Match 赛程表
字段名称 字段类型 描述
matchID int 主键
hostTeamID int 主队的ID
guestTeamID int 客队的ID
matchResult varchar(20) 比赛结果,如(2:0)
matchTime date 比赛开始时间
  1. Team 参赛队伍表
字段名称 字段类型 描述
teamID int 主键
teamName varchar(20) 队伍名称
  1. MatchhostTeamIDguestTeamID都与Team中的teamID关联
  2. 查出 2006-6-1 2006-7-1之间举行的所有比赛,并且用以下形式列出:
  3. 拜仁 20 不来梅 2006-6-21
  4.  
  5. mysql> select * from m;
  6. +-----+------+------+------+------------+
  7. | mid | hid | gid | mres | matime |
  8. +-----+------+------+------+------------+
  9. | 1 | 1 | 2 | 2:0 | 2006-05-21 |
  10. | 2 | 2 | 3 | 1:2 | 2006-06-21 |
  11. | 3 | 3 | 1 | 2:5 | 2006-06-25 |
  12. | 4 | 2 | 1 | 3:2 | 2006-07-21 |
  13. +-----+------+------+------+------------+
  14. 4 rows in set (0.00 sec)
  15.  
  16. mysql> select * from t;
  17. +------+----------+
  18. | tid | tname |
  19. +------+----------+
  20. | 1 | 国安 |
  21. | 2 | 申花 |
  22. | 3 | 传智联队 |
  23. +------+----------+
  24. 3 rows in set (0.00 sec)
  25.  
  26. mysql> select hid,t1.tname as hname ,mres,gid,t2.tname as gname,matime
  27. -> from
  28. -> m left join t as t1
  29. -> on m.hid = t1.tid
  30. -> left join t as t2
  31. -> on m.gid = t2.tid;
  32. +------+----------+------+------+----------+------------+
  33. | hid | hname | mres | gid | gname | matime |
  34. +------+----------+------+------+----------+------------+
  35. | 1 | 国安 | 2:0 | 2 | 申花 | 2006-05-21 |
  36. | 2 | 申花 | 1:2 | 3 | 传智联队 | 2006-06-21 |
  37. | 3 | 传智联队 | 2:5 | 1 | 国安 | 2006-06-25 |
  38. | 2 | 申花 | 3:2 | 1 | 国安 | 2006-07-21 |
  39. +------+----------+------+------+----------+------------+
  40. 4 rows in set (0.00 sec)
  41.  
  42. 6 union查询
  43. 6.1:把ecs_comment,ecs_feedback两个表中的数据,各取出4列,并把结果集union成一个结果集.
  44.  
  45. 6.2:3期学员碰到的一道面试题
  46. A表:
  47. +------+------+
  48. | id | num |
  49. +------+------+
  50. | a | 5 |
  51. | b | 10 |
  52. | c | 15 |
  53. | d | 10 |
  54. +------+------+
  55.  
  56. B表:
  57. +------+------+
  58. | id | num |
  59. +------+------+
  60. | b | 5 |
  61. | c | 15 |
  62. | d | 20 |
  63. | e | 99 |
  64. +------+------+
  65.  
  66. mysql> # 合并 ,注意all的作用
  67. mysql> select * from ta
  68. -> union all
  69. -> select * from tb;
  70. +------+------+
  71. | id | num |
  72. +------+------+
  73. | a | 5 |
  74. | b | 10 |
  75. | c | 15 |
  76. | d | 10 |
  77. | b | 5 |
  78. | c | 15 |
  79. | d | 20 |
  80. | e | 99 |
  81. +------+------+
  82.  
  83. 要求查询出以下效果:
  84. +------+----------+
  85. | id | sum(num) |
  86. +------+----------+
  87. | a | 5 |
  88. | b | 15 |
  89. | c | 30 |
  90. | d | 30 |
  91. | e | 99 |
  92. +------+----------+
  93.  
  94. 参考答案:
  95. mysql> # sum,group求和
  96. mysql> select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id;
  97. +------+----------+
  98. | id | sum(num) |
  99. +------+----------+
  100. | a | 5 |
  101. | b | 15 |
  102. | c | 25 |
  103. | d | 30 |
  104. | e | 99 |
  105. +------+----------+
  106. 5 rows in set (0.00 sec)
  107.  
  108. 7: 子查询:
  109. 7.1:查询出最新一行商品(以商品编号最大为最新,用子查询实现)
  110. select goods_id,goods_name from
  111. ecs_goods where goods_id =(select max(goods_id) from ecs_goods);
  112.  
  113. 7.2:查询出编号为19的商品的栏目名称(用左连接查询和子查询分别)
  114. 7.3:用where型子查询把ecs_goods表中的每个栏目下面最新的商品取出来
  115. select goods_id,goods_name,cat_id from ecs_goods where goods_id in (select max(goods_id) from ecs_goods group by cat_id);
  116. 7.4:用from型子查询把ecs_goods表中的每个栏目下面最新的商品取出来
  117. select * from (select goods_id,cat_id,goods_name from ecs_goods order by goods_id desc) as t group by cat_id;
  118.  
  119. 创建触发器:
  120.  
  121. CREATE trigger tg2
  122. after insert on ord
  123. for each row
  124. update goods set goods_number=goods_number-new.num where id=new.gid
  125.  
  126. CREATE trigger tg3
  127. after delete on ord
  128. for each row
  129. update goods set goods_number=good_number+old.num where id=old.gid
  130.  
  131. CREATE trigger tg4
  132. after update on ord
  133. for each row
  134. update goods set goods_number=goods_number+old.num-new.num where id=old.gid

2012-03-25更新,添加了面试案例

msyql 笔记的更多相关文章

  1. msyql笔记

    CREATE TABLE class ( cid int(11) NOT NULL AUTO_INCREMENT, caption varchar(32) NOT NULL, PRIMARY KEY ...

  2. MySQL入门笔记

    MySQL入门笔记 版本选择: 5.x.20 以上版本比较稳定 一.MySQL的三种安装方式: 安装MySQL的方式常见的有三种: ·          rpm包形式 ·          通用二进制 ...

  3. Hbase—学习笔记(一)

    此文的目的: 1.重点理解Hbase的整体工作机制 2.熟悉编程api,能够用来写程序 1.  什么是HBASE 1.1.   概念特性 HBASE是一个数据库----可以提供数据的实时随机读写 HB ...

  4. 笔记-mysql-管理及基础操作

    笔记-mysql使用-管理及基础操作 1.      简介 mysql是一个免费的关系型数据库,不过好像被oracle收购了.... 希望它继续免费. 1.1.    相关术语 数据库,表,列,行,冗 ...

  5. go微服务框架kratos学习笔记十(熔断器)

    目录 go微服务框架kratos学习笔记十(熔断器) 什么是熔断 熔断器逻辑 kratos Breaker kratos 熔断逻辑 kratos熔断器使用说明 bladmaster client br ...

  6. Vue学习笔记-rest_framework_jwt 学习

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  7. Vue学习笔记-Django REST framework3后端接口API学习

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  8. Vue学习笔记-vue-element-admin 前端学习

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  9. Vue学习笔记-vue-element-admin 按装报错再按装

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

随机推荐

  1. 【ERROR】Oracle11g两个监听同名进程的故障

    问题: 一个实例启动了另个两个监听. 解决方法: #ps -ef | grep tnslsnr #oracle 925826 1 0 Apr 06 - 234:50 /u01/app/oracle/p ...

  2. PLSQL_统计信息系列07_数据字典统计信息的导出和导入

    2014-05-05 Created By BaoXinjian

  3. Python 字典 keys() 方法

    描述 Python 字典 keys() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回字典中的所有的键. 语法 keys() 方法语法: D.keys() 参数 无. 返回值 ...

  4. Python istitle() 方法

    描述 istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写. 语法 istitle() 方法语法: S.istitle() 参数 无. 返回值 如果字符串中所有的单词拼 ...

  5. USB的中断说明

    STM32的USB模块可以产生三种中断:USB唤醒中断.USB高优先级中断和USB低优先级中断,在STM32的参考手册中没有详细说明这三种中断对应哪些事件,现说明如下: 1)USB唤醒中断:在中断向量 ...

  6. [svc]cisco ipsec使用证书认证

    基础配置 用的c7200-adventerprisek9-mz.151-4.M2.bin - R1 conf t int f0/0 ip add 202.100.1.1 255.255.255.0 n ...

  7. 【Android】3.13 路径规划功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 线路规划支持以下功能: 公交信息查询:可对公交详细信息进行查询: 公交换乘查询:根据起.终点,查询策 ...

  8. app 图标规格参考表

    转自:http://www.cocoachina.com/appstore/top/2012/1105/5031.html 像我一样记不住iOS应用图标像素尺寸的开发者不在少数,我经常需要查询不同设备 ...

  9. windows xp 创建 Oracle(11G)数据库实例时写入系统日志失败解决方案

    1.查看Windows日志相关服务(Even Log),无异常: 2.清空事件管理器中“应用程序”和“系统节点”的内容,对这两个节点,分别在“属性”页面用增大“最大日志文件大小”的值,并选中“按需要改 ...

  10. 部署hadoop2.7.2 集群 基于zookeeper配置HDFS HA+Federation

    转自:http://www.2cto.com/os/201605/510489.html hadoop1的核心组成是两部分,即HDFS和MapReduce.在hadoop2中变为HDFS和Yarn.新 ...