gpt4 book ai didi

mySQL:使用条件计算多个表中的唯一键

转载 作者:行者123 更新时间:2023-11-29 15:46:49 25 4
gpt4 key购买 nike

我有一堆表(“table001”、“table002”等),其中有大量行和多列。

列是“id”(键)、“mode”(“ui”或“cmd”)和日期。

我想找出所有表中存在多少个唯一的“id”,其模式设置为“ui”且日期小于 30 天前。

这是我当前的查询:

SELECT COUNT(*) FROM
(
SELECT id FROM table001 WHERE mode="ui" AND date > (NOW() - INTERVAL 30 DAY)
UNION
SELECT id FROM table002 WHERE mode="ui" AND date > (NOW() - INTERVAL 30 DAY)
UNION
SELECT id FROM table003 WHERE mode="ui" AND date > (NOW() - INTERVAL 30 DAY)
UNION
[...etc etc for 30 tables]

) as t

这是正确的做法吗?

最佳答案

如果所有 pk id 都称为 id,那么您可以运行下面的存储过程,而无需静态键入所有表:

DELIMITER $$
CREATE PROCEDURE eval(IN dynamic_statement TEXT)
BEGIN
SET @dynamic_statement := dynamic_statement;
PREPARE prepared_statement FROM @dynamic_statement;
EXECUTE prepared_statement;
DEALLOCATE PREPARE prepared_statement;
END$$
DELIMITER ;





DELIMITER $$
CREATE PROCEDURE sp_unique_id_count_across_all_tables()
BEGIN
DECLARE var_key_fieldname VARCHAR(64) DEFAULT 'id';
DECLARE var_tables_count INTEGER UNSIGNED;
DECLARE var_index INTEGER UNSIGNED DEFAULT 1;
DECLARE var_tablename_of_index VARCHAR(64);

DROP TEMPORARY TABLE IF EXISTS tmp_tables_with_id;
CREATE TEMPORARY TABLE tmp_tables_with_id
SELECT
# *
(@cnt := @cnt + 1) AS idx,
TABLE_NAME AS tablename
FROM information_schema.columns
CROSS JOIN (SELECT @cnt := 0) A
WHERE table_schema=SCHEMA()
AND (COLUMN_NAME COLLATE utf8_unicode_ci) = (var_key_fieldname COLLATE utf8_unicode_ci)
;

SET var_tables_count := (SELECT COUNT(0) FROM tmp_tables_with_id);

DROP TEMPORARY TABLE IF EXISTS tmp_all_ids;
CREATE TEMPORARY TABLE tmp_all_ids(
id INTEGER NOT NULL UNIQUE
);
WHILE (var_index <= var_tables_count) DO
SET var_tablename_of_index := (
SELECT tablename
FROM tmp_tables_with_id
WHERE idx = var_index
);
CALL eval(CONCAT('
INSERT IGNORE INTO tmp_all_ids(id)
SELECT
DISTINCT t.', var_key_fieldname, '
FROM ', var_tablename_of_index,' t
-- ON DUPLICATE KEY UPDATE id = t.', var_key_fieldname,'
;
'));
SET var_index := var_index + 1;
END WHILE;
SELECT COUNT(0) FROM tmp_all_ids;
END$$
DELIMITER;




DROP PROCEDURE IF EXISTS eval;
DROP PROCEDURE IF EXISTS sp_unique_id_count_across_all_tables;


CALL sp_unique_id_count_across_all_tables();

关于mySQL:使用条件计算多个表中的唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56966059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com