简体   繁体   English

如何在SQL Server 2012中为第一张表的每个ID获取电子邮件ID(一个值),然后将第二张表的电子邮件名称显示为逗号?

[英]How to get Email ID(one value) for each ID for 1st table and then display Email Name from 2nd table as comma Separated in SQL Server 2012?

What i have tried Results wanted like this From One table , for each ID there can be multiple email id's based on some condition Ex 我尝试过的 结果想要这样的结果从一个表开始,对于每个ID,根据某些条件,可以有多个电子邮件ID,例如

ID  EmailID's
1    Mike.Foster@Mail.com
1    lilly.Foster@Mail.com
2    Michel.Josh@Mail.com
2    Nash.Ted@Mail.com

I have to get email Name from these Ids from another table, something like this 我必须从另一个表中的这些ID中获取电子邮件名称,就像这样

Output i Need 我需要的输出

Email_Name
Foster.Mike,Foster.Lilly
Josh.Michel,Ted.Nash

This is what i tried. 这就是我尝试过的。

  SELECT  User_Email = 
    STUFF((SELECT DISTINCT ', ' + User_Email
           FROM table1 b 
           WHERE b.Component_ID= a.Component_ID
           and [Role] ='Team Lead' and Functional_Group ='Product'
          FOR XML PATH('')), 1, 2, '')
FROM [WFS].table1 a
GROUP BY table1

Now another table i want Email Names 现在我要另一个表电子邮件名称

 Select EmailNamefrom Table2 where EmailIDs IN ( 'code for Email')

Table1 schema 表1模式

ID Component_ID EmailIDs               Role        Functional_Group 
1    1          Mike.Foster@Mail.com   Team Lead    Product
2    1          lilly.Foster@Mail.com  Team Lead    Product
3    2          Michel.Josh@Mail.com   Team Lead    Product
4    2          Nash.Ted@Mail.com      Team Lead    Product

Table 2 schema 表2模式

ID  EmailIDs                EmailName
 1   Mike.Foster@Mail.com  Foster.Mike
 2   lilly.Foster@Mail.com Foster.Lilly

Any suggestions are welcome.Thanks in advance 欢迎任何建议。谢谢

You were actually close but your SQL doesn't match your schema. 您实际上已经关闭,但是您的SQL与您的架构不匹配。 This one works as you want: 这是您想要的作品:

SELECT  Email_Name = 
    STUFF((SELECT DISTINCT ', ' + EmailIDs
           FROM table1 b 
           WHERE b.Component_ID= a.Component_ID
           and [Role] ='Team Lead' and Functional_Group ='Product'
          FOR XML PATH('')), 1, 2, '')
FROM table1 a
GROUP BY a.Component_ID;

EDIT: I didn't understand what you are asking exactly. 编辑:我不明白你到底在问什么。 Might this be what you meant? 可能这就是您的意思吗?

SELECT STUFF((SELECT ', ' + EmailName
FROM Table2 where EmailIDs IN ( SELECT EmailIDs
           FROM table1 
           WHERE [Role] ='Team Lead' and Functional_Group ='Product')
           FOR XML PATH('')), 1, 2, '')

Or you meant this: 或者你的意思是这样的:

SELECT DISTINCT Component_ID, emailNames
FROM table1
CROSS APPLY (SELECT STUFF((SELECT ', '+t2.EmailName
           FROM table2 t2
           INNER JOIN TABLE1 t1 ON t1.EmailIDs = t2.EmailIDs
           WHERE t1.Component_ID = Table1.Component_ID
           FOR XML PATH('')), 1, 2, '')
 ) t(EmailNames)
WHERE [Role] ='Team Lead' and Functional_Group ='Product'

Here is SQLFiddle Demo 这是SQLFiddle演示

Disregard this answer as I found out that GROUP_CONCAT() is a MySQL specific function , which means it won't work in SQL Server, however, I'll let it stay for future references. 当我发现GROUP_CONCAT()是MySQL特定的函数时 ,请忽略此答案 ,这意味着它在SQL Server中将不起作用,但是,我将其留作以后参考。

SELECT
  GROUP_CONCAT(EmailName SEPARATOR ', ') as name
FROM
  table1
INNER JOIN table2 ON table1.EmailIDs=table2.EmailIDs
WHERE
  table1.EmailIDs=table2.EmailIDs
GROUP BY
  table1.Component_ID

Output: 输出:

Foster.Mike, Foster.Lilly 福斯特·迈克,福斯特·莉莉

Ted.Nash, Josh.Michel 泰德·纳什,乔什·米歇尔

Working SQL fiddle 工作的SQL小提琴

Docs: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat 文件: https//dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat

You need CTE with STUFF() : 您需要使用STUFF() CTE

WITH t AS (
     SELECT t1.*, t2.emailname
     FROM table1 t1 INNER JOIN
          table2 t2
          ON t2.emailids = t1.emailids 
)

SELECT id, STUFF ( (SELECT DISTINCT ','+t1.emailname
                    FROM t t1
                    WHERE t1.id = t.id
                    FOR XML PATH ('')
                   ), 1, 1, ''
                 ) AS Email_Name
FROM t
GROUP BY id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从第二个表中显示2个不同的ID从第一个表中显示ID - How to show id from 1st table from 2 different ids from 2nd table 如何在 SQL 服务器中从第一个和第二个表中获取匹配记录,并且仅从第一个表中获取不匹配记录,该服务器已加入 1 个字段 - How to get a a matching records from 1st and 2nd table and only non matching records from 1st table in SQL Server having joined by 1 field 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? 根据第一张表的ID汇总第二张表中的值 - Summing the values from the 2nd table based on ID of the 1st table Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table? 如何在另一个表中选择具有相同值的SQL表中的第一行和第二行实例? - How to select 1st and 2nd row instance in SQL table having the same value in another table? 从 SQL Server 表中的带连字符的字符串列中提取第 1 个第 2 个第 3 个值直到第 n 个值 - Extract 1st 2nd 3rd values till nth value from hyphenated string column in SQL Server table SQL查询根据第一个表的输入从第二个表中获取字段 - SQL query to get fields from 2nd table based on input from 1st 如果第 2 个表中存在第 1 个表的值,则用第 1 个和第 2 个表的总和更新第 2 个表的值,否则将第一个表数据插入到第 2 个表 - If value of 1 table exists in the 2nd table, update the 2nd table value with sum of 1st and 2nd tables, else insert 1st table data to 2nd table 将数据从第三张表链接到第一张表和第二张表 - Link data from third table to a 1st and a 2nd table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM