mysql - 给出 MySQL 表中 Null 值的位置

标签 mysql sql

我有一个混合了空值和非空值的表,我想在迁移到其他源之前比较空值在表中的位置

mysql> select * from student2;
+------+---------+------------+
| id   | name    | address    |
+------+---------+------------+
|   10 | Rams    | NULL       |
|   12 | Hari    | NULL       |
|   13 | Shyam   | NULL       |
|   14 | NULL    | Hyderabad  |
|      | Revathy | Singapore  |

我期望输出只有 Null 值的位置以及行号,即

Row Number 1 Column Number 3
Row Number 2 Column Number 3
Row Number 3 Column Number 3
Row Number 4 Column Number 1
Row Number 5 Column Number 1

最佳答案

我认为这个查询可以帮助您:

SELECT (SELECT COUNT(*)
        FROM student2 si
        WHERE si.id <= s.id) As RowNo,
       CASE WHEN s.name IS NULL THEN 1
            WHEN s.address IS NULL THEN 2
       END As ColumnNo
FROM student2 s
WHERE COALESCE(s.name, s.address, '0') <> '0' 

关于mysql - 给出 MySQL 表中 Null 值的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129828/

相关文章:

mysql - 如何在 VB 中使用 MySQL 数据库验证登录?

mysql - Railed 数据库嵌套搜索混淆 : part 2

MySQL 从表中获取出现次数最多的成员

c# - LINQ 从多个连接中获取具有最大日期的记录

MySQL:选择上面的行

MySQL - 无法在 mysql 5.7 上使用 REFERENCES(但在 5.0 上工作)

php - 在 PHPMyadmin 中的多列上添加 FK 约束

mysql - 如何提高大表中的插入时间?

mysql - 解析 Json - 从 sql (Aurora) 到 aws lambda

sql - Ruby 数组作为普通 SQL 查询的参数