我是MySQL的新手。现在,我有40张桌子。它们的一部分包含一个名为“ case_id”(字符)的字段。我想查找哪些表具有“ case_id”列,但没有case_id =“ 123”。
我只选择所有具有“ case_id”列的表,但是我不知道如何从中查找所需的表。
您有什么建议吗?非常感谢!
select DISTINCT TABLE_NAME as test_table from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME IN ("case_id") and TABLE_SCHEMA="test_db";
例如,
表A的记录为“ case_id” =“ 123”。因此,它不会被打印。 表B不包含“ case_id”列,也不会打印。 表C具有“ case_id”,但没有“ case_id” =“ 123”的记录,将打印名称“ Table C”。
编辑#2
只需根据@abk的答案更新我的代码。我只是更改了方案的名称。
CREATE DEFINER=`root`@`localhost` PROCEDURE `MISS_CASE_CHECK`()
BEGIN
DECLARE Table_name TEXT;
DECLARE done INT DEFAULT 0;
DECLARE str LONGTEXT;
DECLARE my_cursor CURSOR FOR
SELECT DISTINCT TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ("case_id") and TABLE_SCHEMA="wes_bk";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN my_cursor;
SET str = '';
WHILE done = 0 DO
BEGIN
FETCH my_cursor INTO Table_name;
SET str = CONCAT(str,Table_name,',');
END;
END WHILE;
SELECT LEFT(str, LENGTH(str) - 1);
CLOSE my_cursor;
SELECT str;
END
不幸的是,我收到了许多错误消息:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE done INT DEFAULT 0' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE str LONGTEXT' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE my_cursor CURSOR FOR
SELECT DISTINCT TABLE_NAME
From INFORMATION' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPEN my_cursor' at line 1
ERROR 1193 (HY000): Unknown system variable 'str'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE done = 0 DO
BEGIN
FETCH my_cursor INTO Table_name' at line 1
ERROR 1193 (HY000): Unknown system variable 'str'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END WHILE' at line 1
ERROR 1054 (42S22): Unknown column 'str' in 'field list'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CLOSE my_cursor' at line 1
ERROR 1054 (42S22): Unknown column 'str' in 'field list'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
我的mysql版本是5.7.12。 有什么建议吗?
答案 0 :(得分:1)
存储过程必须看起来像这样。
table_name将被放置在一个文本变量中,并且您看到的结果是逗号分隔的文本。
如果您需要一个包含行的表,则必须添加一个临时表并在末尾选择该表。
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `MISS_CASE_CHECK`()
BEGIN
DECLARE Tablename TEXT;
DECLARE done INT DEFAULT 0;
DECLARE str LONGTEXT;
DECLARE my_cursor CURSOR FOR
SELECT DISTINCT TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME ='Id' and TABLE_SCHEMA='testdb';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET @tablen = '';
OPEN my_cursor;
SET str = '';
WHILE done = 0 DO
BEGIN
FETCH my_cursor INTO Tablename;
SET str = CONCAT(str,Tablename,',');
END;
END WHILE;
SELECT LEFT(str, LENGTH(str) - 1);
CLOSE my_cursor;
END//
DELIMITER ;
答案 1 :(得分:0)
您将无法一步一步完成此操作(除非有一些超级向后的创造方式)。取而代之的是,您将执行现有查询以获取作为第二个条件候选者的表的列表。然后,您将对该列表中的每个表发出一条SQL语句,以测试该表是否具有您的值。
您可以通过将第一个SQL语句作为一个大的UNION QUERY编写其他SQL语句来加快此速度,然后执行第二步。
类似的东西:
select DISTINCT
CONCAT('SELECT DISTINCT ', TABLE_NAME, ' AS table_name FROM ', TABLE_NAME, ' WHERE NOT EXISTS (SELECT 1 FROM ', TABLE_NAME, ' WHERE case_id=123) UNION ALL ') as sqlstatement
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME IN ("case_id") and TABLE_SCHEMA="test_db";
只需获取该输出,修剪最后的UNION ALL
并提交(我尚未测试过,但从理论上讲,这应该可以完成工作)
如果您经常这样做,则应将其移至存储过程或其他脚本语言中,在其中循环搜索候选对象(来自原始SQL),然后发出sql查询该表以查看是否拥有case_id
,并将其添加到新表/列表/数组/内容中以进行输出。