mysql-sql高级应用
2018-06-17 22:49:07来源:未知 阅读 ()
- select * from play_list order by createtime; - select * from play_list order by bookedcount desc,createtime asc;
select distinct userid from play_list; select distinct userid,play_name from play_list;
(userid,play_named都相同时去重)
mysql> select userid,count(*) AS play_num from play_list group by userid having count(*)>=2;
select * from play_list where play_name like '%男孩%';
通配符
|
描述
|
%
|
代替一个或多个字符
|
_
|
替代单个字符
|
[charlist]
|
中括号中的任何单一字符
|
[^charlist]
或者
[!charlist]
|
不在中括号中的任何单一字符
|
select * from play_list where (createtime between 1427701323 and 1430383307) limit 10 offset 6
mysql> select play_name,case when trackcount is null then 0 else trackcount end from play_list;
mysql> SELECT play_fav.userid FROM play_fav INNER JOIN play_list ON play_fav.play_id = play_list.id where play_list.play_name = '老男孩';
mysql> select f.userid from play_list lst,play_fav f where lst.id = f.play_id and lst.play_name = '老男孩'
select userid from play_fav where play_id=(select id from play_list where play_name = '老男孩');
mysql> select userid from play_list -> union -> select userid from play_fav;
- 多值插入:insert into table values(.....),(.....)
- 覆盖插入:replace into table values(...)
- 忽略插入:insert ignore into table values(...)
- 查询插入:insert into table_a select * from table_b
mysql> insert into a values(1,100) on duplicate key update age=100;
mysql> alter table order add primary key (id);
mysql> select * from a; +----+------+ | id | age | +----+------+ | 1 | 100 | | 2 | 34 | | 3 | 23 | | 4 | 29 | +----+------+ mysql> select * from b; +------+------+------+ | id | name | age | +------+------+------+ | 1 | pw | 20 | | 2 | ljb | 30 | +------+------+------+ mysql> update a,b set a.age=b.age where a.id = b.id; mysql> select * from a; +----+------+ | id | age | +----+------+ | 1 | 20 | | 2 | 30 | | 3 | 23 | | 4 | 29 | +----+------+
mysql> select * from a; +----+------+ | id | age | +----+------+ | 1 | 20 | | 2 | 30 | | 3 | 23 | | 4 | 29 | +----+------+ mysql> select * from b; +------+------+------+ | id | name | age | +------+------+------+ | 1 | pw | 20 | | 2 | ljb | 30 | +------+------+------+ mysql> delete a from a,b where a.id=b.id and b.name='pw'; mysql> select * from a; +----+------+ | id | age | +----+------+ | 2 | 30 | | 3 | 23 | | 4 | 29 | +----+------+
DELETE FROM Customers WHERE cust_id = '1000000006';
- 目标:掌握常用的mysql聚合函数,预定义函数
- 在SQL查询语句中运用上述函数结构group by,order by等语法完成各种统计功能
- 聚合函数面向一组数据,对数据进行聚合运算后返回单一的值
- mysql聚合函数基本语法:select function(列) from 表
mysql> select song_name,max(playcount) from song_list; //错误查法 #select song_name,没有对应 playcount; #注意聚合函数是对返回列来做处理的,此中放回列是所有歌曲; mysql> select song_name,playcount from song_list order by playcount desc limit1;//正确 子查询方法: select song_name from song_list where playcount=(select max(playcount) from song_list);
mysql> select album,group_concat(song_name) from song_list group by album; +------------------+-------------------------------------------------+ | album | group_concat(song_name) | +------------------+-------------------------------------------------+ | 1701 | 大象,定西 | | Straight Shooter | Good Lovin' Gone Bad,Weep No More,Shooting Star | | 作品李宗盛 | 风柜来的人 | | 红雪莲 | 红雪莲 | +------------------+-------------------------------------------------+
- 预定义函数面向单一值数据,返回一对一的处理结果(聚合函数可以理解成多对一)
- 预定义函数基本语法:select function(列) from 表;select * from 表 where 列 = function(value)
- order by
- distinct
- limit offset:
- case when then else end
- 连接-join两种写法
- 子查询为什么不利于优化:优化器不能改变,驱动表,内层表;从而不能优化;驱动表一般表量较小,因其需要全表id;内层表,仅需要查找一个或几个索引;这就是jion后优化器工作
- union:把不同表中相同字段聚合在一个结果集中
- 连表update,根据B表age值更新A表age:update a,b set a.age=b.age where a.id = b.id;
- 连表delete,根据B表name删除A表的数据:delete a from a,b where a.id=b.id and b.name='pw';
- 聚合函数:AVG(),COUNT(),COUNT(DISTNCT),MAX(),MIN(),SUM()常与 group by,order by连用;
- group_concat()mysql特有
- 预定义函数
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:MySQL高可用之MHA (转)
- PHP代码审计,你会吗? 2019-09-04
- laravel结合workerman开发在线聊天应用 2019-09-02
- Redis在Laravel项目中的应用实例详解 2019-08-23
- 应用系统之间数据传输的四种方式 2019-08-23
- PHP常用的五种设计模式及应用场景,来了解下 2019-08-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash