Hello,大家好!我是程序員阿飛!今天主要給大家分享一下Mysql中常見的一些知識點,稍微整理一下。
一、Mysql中如何查找爲空或者不爲空的數據
1、查詢爲空的方法
select * from table_name where id is null(爲null)
select * from table_name where id = ' '(等於空字符串)
2、查詢不爲空的犯法
select * from table_name Where id is not null (不爲null)
select * from table_name where id != ' ' (不等於空字符串)
select * from table_name where id <> ' ' (不等於空字符串)
二、Mysql中concat、concat_ws、group_concat的區別
1、concat:CONCAT(str1,str2,…) 返回結果爲連接參數產生的字符串。如有任何一個參數爲NULL ,則返回值爲 NULL。例如:select concat('11','22','33'); 結果:112233
2、concat_ws:代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一個參數是其它參數的分隔符。分隔符的位置放在要連接的兩個字符串之間。分隔符可以是一個字符串,也可以是其它參數。例如:select concat_ws(',','11','22','33');結果:11,22,33
3、group_concat:group_concat([DISTINCT] 要連接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])例如:select id,group_concat(name) from aa group by id;結果: |1 | 10,20,20|; |2 | 20 | ;|3 | 200,500|
三、repeat()函數
1、用來複制字符串,如下'ab'表示要複製的字符串,2表示複製的份數
例如:select repeat('ab',2);結果:abab
四、mysql查詢出數據更新到另一張表
需求:從A表查詢出字段數據,更新到B表對應的字段
例子:當table_a表中的b_id=table_b表的id時,將table_b表中的sn,filed1更新到table_a 表中sn,filed1字段
-- 更新數據
UPDATE `table_a` r
INNER JOIN (SELECT id,sn,filed1 FROM `table_b`) t ON r.b_id= t.id
SET r.sn = t.sn,r.filed1 = t.filed1
五、mysql中ifnull()方法的用法
1、表達式:IFNULL(expr1,expr2)
2、含義:如果expr1不是NULL,IFNULL()返回expr1,否則它返回expr2。IFNULL()返回一個數字或字符串值
3、示例:select ifnull(name,'no name') from person;
4、延伸:將查詢到的結果中的null轉化爲指定的字符串,但是其實在where查詢部分也可以使用ifnull():select * from person where ifnull(name,'no name')='no name';
【參考網址】
https://www.cnblogs.com/benpao/p/7208671.html