gpt4 book ai didi

mysql - 如何用相同的表列编写sql条件

转载 作者:行者123 更新时间:2023-11-29 07:32:53 25 4
gpt4 key购买 nike

我有一个名为 fee_category 的表

+------------+--------------+-----------+
| fee_cat_id | fee_cat_name | parent_id |
+------------+--------------+-----------+
| 1 | days scholar | 0 |
| 2 | tuition | 1 |
| 3 | exam | 1 |
| 4 | bus | 1 |
+------------+--------------+-----------+

我想根据parent_id fee_type 获取如下

如果 parent_id=1

然后

feetype=days scholar

我试过如下

SELECT fee_cat_id
, fee_cat_name
, fee_cat_name IN
( select fee_cat_name
from fee_category
where parent_id !=0
) fee_type
from fee_category;

我的输出看起来像这样

+------------+--------------+----------+
| fee_cat_id | fee_cat_name | fee_type |
+------------+--------------+----------+
| 1 | days scholar | 0 |
| 2 | tuition | 1 |
| 3 | exam | 1 |
| 4 | bus | 1 |
+------------+--------------+----------+

我要输出如下

+------------+--------------+----------  +
| fee_cat_id | fee_cat_name | fee_type |
+------------+--------------+---------- +
| 1 | days scholar | none |
| 2 | tuition |days scholar|
| 3 | exam |days scholar|
| 4 | bus |days scholar|
+------------+--------------+----------+

最佳答案

您可以为表格命名。

SELECT a.fee_cat_id, a.fee_cat_name, COALESCE(b.fee_cat_name, 'NONE') as fee_type 
FROM fee_category a
LEFT OUTER JOIN fee_category b
ON a.fee_type = b.fee_cat_id;

关于mysql - 如何用相同的表列编写sql条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50350413/

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