gpt4 book ai didi

mysql - 在 SQL 中使用子字符串的问题

转载 作者:行者123 更新时间:2023-11-29 12:37:46 26 4
gpt4 key购买 nike

我在针对某个问题进行 SQL 查询时遇到了问题:

表1

Client ID|Name of Client|Customer Type|Account Type|

01| A | AB | 30 |
02| B | AB | 30 |
03| C | CD | 30 |
04| D | CF | 40 |

表2

Argument|Switch|

AB30 | Y |
CD30 | N |
CF40 | Y |

表3

ClientID|Name Of Client|External_Information|

01|A | External A
02|B | External B
03|C | External C

目前,我正在尝试根据 3 个不同的表提取信息。我想根据 3 个条件从表 3 中提取外部信息

  1. 客户端 ID 必须存在于表 3 和表 1 中
  2. 如果表 3 和表 1 中存在客户 ID,那么我们必须在表 2 中检查客户类型帐户类型的组合(例如:对于客户ID 01 ,组合为 AB30)表 1 中的值存在于表 2 中,并且如果表 2 中的Switch =“Y”。

我当前的 SQL 是:

select External_Information 
from Table_3 a
where a.ClientID = (select ClientID
from Table_1
where ClientID = a.ClientID)

这可以解决条件 1 ,但对于条件 2,我不确定如何在同一个 SQL 查询中做到这一点。

预期结果应该是:

External_Information
-----------------------
External A
External B

最佳答案

使用 JOIN 并使用 CONCAT() 组合表 1 中的两列。

SELECT a.`External_Information`
FROM Table_3 AS a
JOIN Table_1 AS b ON a.`ClientID` = b.`Client ID`
JOIN Table_2 AS c ON CONCAT(b.`Customer Type`, b.`Account Type`) = c.Argument
WHERE c.Switch = 'Y'

DEMO

关于mysql - 在 SQL 中使用子字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26520234/

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