mysql 常用语句整理

时间:2021-07-20 01:58:30

1. 替换某个字段中的部分内容

SET @oldlink1 = ‘http://192.168.100.66‘;
SET @newlink1 = ‘www.baidu.com‘;

UPDATE Test1
SET Content = (
	REPLACE (
		Content ,@oldlink1 ,@newlink1
	)
)
WHERE
	Content LIKE concat(‘%‘ ,@oldlink1, ‘%‘);

 

2.连表删除数据

SET @UnitId = ‘27542‘;

DELETE t1
FROM
	Test1 AS t1
INNER JOIN Test2 AS t2 ON t1.Code = t2.Code
WHERE
	t2.UnitId NOT IN (@UnitId);

  

3.连表插入数据

INSERT INTO Test (
	ID,
	PostTime
) SELECT
	UUID() AS ID,
	t1.PostTime
FROM
	Test1 AS t1
INNER JOIN Test2 AS t2 ON t1.Code = t2.Code;

 

4. 连表更新数据

UPDATE Test1 AS t1
INNER JOIN Test2 AS t2 ON t1.Code = t2.Code
SET t1.NextFlowNodeId = 72
WHERE
	t1.ProcessStepId = 70;

 

5.获取某个库下的所有表名

select table_name from information_schema.tables where [email protected]

  

6.获取某个表的所有字段名

select COLUMN_NAME from information_schema.COLUMNS where table_name = @tableName and table_schema = @dbName;

 

7.CONCAT 拼接字符串

SET @SearchText= CONCAT(‘%‘,‘测试‘,‘%‘);
SELECT * FROM  Test3 WHERE CONCAT(ItemName,ItemNumber,DeptName,RealName) LIKE @SearchText;

 注意:若拼接项中存在NULL值,结果为NULL。如 CONCAT(‘测试‘,‘001‘,NULL,‘小明‘),结果为NULL,并不是我们想要的结果:"测试001小明"