说明:从知乎等平台摘下来的知识点,常点开看看呦。
MySQL部分:
逆流:001-MySQL数据库与客户端navicat安装
逆流:002-MySQL入门:数据库,表的操作和SQL语言
逆流:003-MySQL:简单查询
逆流:004-MySQL:汇总分析
自己添加的知识点(通过刷题获得):
1、SUBSTRING(s,n,len)获取子串函数
从字符串 s 返回一个长度同 len 字符相同的子字符串,起始于位置 n。也可能对 n 使用一个负值。假若这样,则子字符串的位置起始于字符串结尾的第 n 个字符,即倒数第 n 个字符,而不是字符串的开头位置。
mysql> SELECT SUBSTRING('computer',3) AS col1,
-> SUBSTRING('computer',3,4) AS col2,
-> SUBSTRING('computer',-3) AS col3,
-> SUBSTRING('computer',-5,3) AS col4;
+--------+------+------+------+
| col1 | col2 | col3 | col4 |
+--------+------+------+------+
| mputer | mput | ter | put |
+--------+------+------+------+
1 row in set (0.00 sec)
注:在mysql中测试可以使用substring函数,但是在牛客网编程中,却只能使用substr函数。
小贴士:
数字代表第几个字符,长度为几,这里没有第0个字符的概念。
另:测试SQL语句可以用SQL命令行客户端,不用workbench,语句要以分号为结尾,才能认为语句结束。
2、GROUP_CONCAT函数,组内拼接函数
用法:group_concat([DISTINCT] 要连接的字段 [ORDER BY ASC/DESC 排序字段] [SEPARATOR '分隔符'])
注:这个函数参数中间没有逗号,是空格。
基本查询:
mysql
以id分组,把name字段的值打印在一行,逗号分隔(默认):
mysql> select id,group_concat(name)
mysql> from aa
mysql> group by id;
+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,分号分隔:
mysql> select id,group_concat(name SEPARATOR ';')
mysql> from aa
mysql> group by id;
+------+----------------------------------+
| id| group_concat(name separator ';') |
+------+----------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
+------+----------------------------------+
3 rows in set (0.00 sec)
注:这个在牛客网编程没有通过,改成group_concat(name , ';') 就可以通过。
以id分组,把去重复的name字段的值打印在一行,逗号分隔:
mysql> select id,group_concat(DISTINCT name)
mysql> from aa
mysql> group by id;
以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序:
mysql> select id,group_concat(name ORDER BY name DESC)
mysql> from aa
mysql> group by id;