MySQL学习笔记六:基本DML操作

1.查询语句,语法如下:

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

例如:

"select songs.songId from (songs,songers,songtags,languages) left join albums_songs on songs.songid=albums_songs.songid
left join album on albums_songs.albumid=album.albumid where songs.songTagId=songtags.songTagId and songs.songerid = songers.songerid and songs.lanid=languages.lanid
and concat(songName,songerName,ifnull(`desc`,''),ifnull(albumName,''),tagContent) like '%$keyword%' and songs.songId not in(select songid from usrdislikesong where usrId=$uid)
";

2.插入操作,使用insert语句,例如:

insert into tb_1(id) values (3);  or
insert into tb_1 select * from tb_2

3.更新操作,使用update语句,例如:

update songs set songName='$songName',songTagId=$tagId,songerId=$songerId,lanId=$lanId,countryAreaId=$areaId where songId=$id

4.删除记录,使用delete操作,例如

delete from songs where songId=$id

5.如果一张表的记录非常大,要将其记录全部删除,可以使用下面的语句

truncate table student  -----使用delete语句效率会比较低------

6.对于各种连接,子查询,排序,分组的查询都是基于以上的语句,结合官方文档就可明白。

子查询

select songId,songSrc,songName from songs where songId=$songid and songs.songId not in(select songid from usrdislikesong where usrId=$uid)

排序

select * from users order by usrid desc limit 10; ---desc 降序 asc 升序---

分组

select count(id) from users_info group by usrGender;
上一篇:windows环境下zookeeper安装和使用


下一篇:【CF497E】Subsequences Return 矩阵乘法