你可以像这样使用动态 SQL
DELIMITER $$
CREATE PROCEDURE sp_empty()
BEGIN
SET @sql = NULL;
SELECT GROUP_CONCAT(
CONCAT('(SELECT GROUP_CONCAT(id) FROM Table1 WHERE `',
column_name, '` IS NULL ',
CASE
WHEN data_type IN('varchar', 'char')
THEN CONCAT('OR `', column_name, '` = ''''')
WHEN data_type IN('date', 'datetime', 'time')
THEN CONCAT('OR `', column_name, '` = 0')
ELSE ''
END, ')`', column_name, '`'))
INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND table_name = 'table1'
AND column_name NOT IN ('id')
GROUP BY table_name;
SET @sql = CONCAT('SELECT ', @sql);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
请注意,它处理不同数据类型的列。
示例用法:
CALL sp_empty();
输出:
| 数据1 | 数据2 | 数据3 |
|-------|-------|--------|
| 2 | 1,4 | 1 |
这是SQLFiddle演示
您显然不一定必须使用存储过程。它只是简化了调用端的事情。你可以做
SET @sql = NULL;
SELECT GROUP_CONCAT(
CONCAT('(SELECT GROUP_CONCAT(id) FROM Table1 WHERE `',
column_name, '` IS NULL ',
CASE
WHEN data_type IN('varchar', 'char')
THEN CONCAT('OR `', column_name, '` = ''''')
WHEN data_type IN('date', 'datetime', 'time')
THEN CONCAT('OR `', column_name, '` = 0')
ELSE ''
END, ')`', column_name, '`'))
INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND table_name = 'table1'
AND column_name NOT IN ('id')
GROUP BY table_name;
SET @sql = CONCAT('SELECT ', @sql);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
这是SQLFiddle演示
如果由于某种原因您不能使用带有动态 SQL 的版本,那么作为最后的手段,您可以自己生成这样的查询,包括所有列
SELECT
(
SELECT GROUP_CONCAT(id)
FROM Table1
WHERE `data1` IS NULL
OR `data1` = ''
) `data1`,
(
SELECT GROUP_CONCAT(id)
FROM Table1
WHERE `data2` IS NULL
) `data2`,
(
SELECT GROUP_CONCAT(id)
FROM Table1
WHERE `data3` IS NULL
OR `data3` = 0
) `data3`
...
这是SQLFiddle演示