一个sql

一个小功能,sql里面用到了一些玩法,记录一下~

SELECT
	id,
	code,
	path,
	(1) AS type
FROM
	department
WHERE
	path LIKE CONCAT(
		(
			SELECT
				path
			FROM
				department
			WHERE
				id = '1'
		),
		'%'
	)
UNION
	(
		SELECT
			id,
			code,
			path,
			(2) AS type
		FROM
			department
		WHERE
			path REGEXP (
				(
					SELECT
						group_concat(path SEPARATOR '|')
					FROM
						department
					WHERE
						id IN (
							SELECT
								id_to
							FROM
								charge_relation
							WHERE
								type_from = '1'
							AND id_from = '2c94978a69b2a9ee0169c7bd198b0449'
							AND type_to = '2'
							AND charge_type = '3'
						)
				)
			)
	)

拼接sql

select CONCAT('update user set code=\'',
		cloud_rbac_jiaojin.`user`.`code`,
		'\' where id=\'',
		cloud_rbac_jiaojin.`user`.id,
		'\'') updates FROM
		cloud_rbac_baomi.`user`,cloud_rbac_jiaojin.`user`
	WHERE
		cloud_rbac_baomi.`user`.username = cloud_rbac_jiaojin.`user`.username

存储过程

DROP PROCEDURE IF EXISTS insert_area;
DELIMITER //
CREATE PROCEDURE insert_area() BEGIN
DECLARE  areaCount VARCHAR(100);
SELECT (SELECT COUNT(1) FROM f_area) INTO areaCount;
IF   (areaCount > 0) 
THEN
   SELECT * FROM `user`;
END IF;
END//
DELIMITER;
CALL insert_area();

你可能感兴趣的:(一个sql)