我需要匹配特殊字符,如何编写查询以获取所需的输出?

huangapple go评论92阅读模式
英文:

I need to match special character, how can I write query to get the required output?

问题

我想要的是仅在供应商和客户表格中常见的邮件,我不想要客户表格中与供应商邮件不匹配的邮件。不幸的是,我们没有类似的ID来进行连接,所以如果可能的话,我们能否只根据邮件进行连接,如果不行的话,我们可以考虑使用共同的ID并进行连接。

以下是您参考的表格和我想要的输出:

我需要匹配特殊字符,如何编写查询以获取所需的输出? 我需要匹配特殊字符,如何编写查询以获取所需的输出? 我需要匹配特殊字符,如何编写查询以获取所需的输出?

英文:

I want mail which is common at supplier's and customer's tables only, I don't
want emails that don't match in customer's table with the supplier's mail. Unfortunately,
we don't have similar IDs to join so if possible can we join with the mail only,
if not then we can consider common IDs and join them.

following tables for your reference and output that I want
我需要匹配特殊字符,如何编写查询以获取所需的输出? 我需要匹配特殊字符,如何编写查询以获取所需的输出?我需要匹配特殊字符,如何编写查询以获取所需的输出?

答案1

得分: 0

正确的解决方案是将数据库设计规范化,不要在同一列中存储多个值。然而,我认为这已经是过去的事情了。

鉴于您拥有的数据,您可以使用 STRING_SPLIT() 将来自 SUPPLIERS 表的电子邮件地址分隔开,然后与 CUSTOMERS 表进行连接。

SELECT S.ID, C.EMAIL
FROM SUPPLIERS S
CROSS APPLY (
   SELECT value AS EMAIL
   FROM STRING_SPLIT(S.EMAIL, ',')
) E
JOIN CUSTOMERS C ON C.EMAIL = E.EMAIL

请参见此db<>fiddle

如果您的数据中可能会包含任何空格,您可能需要将 TRIM() 添加到 STRING_SPLIT() 结果中 - SELECT TRIM(value) AS EMAIL

英文:

The proper solution is to normalize your database design to not store multiple values in the same column. However, I expect that is water under the bridge.

Given the data that you have, you can use STRING_SPLIT() to separate the emails from the SUPPLIERS table and then join with CUSTOMERS.

SELECT S.ID, C.EMAIL
FROM SUPPLIERS S
CROSS APPLY (
   SELECT value AS EMAIL
   FROM STRING_SPLIT(S.EMAIL, &#39;,&#39;)
) E
JOIN CUSTOMERS C ON C.EMAIL = E.EMAIL

See this db<>fiddle

If your data may have any spaces mixed in, you may need to add TRIM() to the STRING_SPLIT() result - SELECT TRIM(value) AS EMAIL.

答案2

得分: 0

选择 CC.ID 和 CC.email from
(
选择 ROW_NUMBER() OVER (按照 ID 升序排列) AS AID, a.*
从 供应商 a
) 作为 AA

左外连接

(
选择 BB.* from
(
选择 ROW_NUMBER() OVER (按照 ID 升序排列) AS BID, b.*
从 客户 b
) 作为 BB
) 作为 CC
在 AA.AID = CC.BID 上

英文:
	Select CC.ID, CC.email from
	(
			SELECT        ROW_NUMBER() OVER (ORDER BY ID ASC) AS AID,a.*
			FROM           Suppliers a
	) as AA

	left outer join 
	(
			Select BB.* from
			(
					SELECT        ROW_NUMBER() OVER (ORDER BY ID ASC) AS BID,b.*
					FROM           Customers b
			) as BB
	) as CC
	on AA.AID = CC.BID

huangapple
  • 本文由 发表于 2023年1月9日 01:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定