gpt4 book ai didi

mysql - 如何根据其他列的结果运行 SELECT 语句?

转载 作者:行者123 更新时间:2023-11-29 05:50:33 24 4
gpt4 key购买 nike

我有一个像这样的 MySQL 表:

CREATE TABLE `my_table` (
`col1` varchar(45) DEFAULT '',
`col2` varchar(45) DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

它包含以下数据:

enter image description here

如何使用 SELECT 语句生成如下结果:

enter image description here

check栏内容含义如下:

`both` when both `col1` and `col2` have values.
`left` when only `col1` has a value.
`rightest` when only `col2` has a value.
`empty` when both `col1` and `col2` has empty value.

我认为查询看起来像这样

# something must be used to replace ???
SELECT (???) AS check, col1, col2 FROM my_table;

??? 应该改成其他的。

最佳答案

使用CASE WHEN表达式

SELECT CASE WHEN col1 = '' and col2='' then 'Empty'
WHEN col1 = '' then 'Right'
WHEN col2='' then 'Left'
else 'Both' end as 'Check',
col1, col2 FROM yourtablename

关于mysql - 如何根据其他列的结果运行 SELECT 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54820060/

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