Sql 如何将不同表中的多行合并为一行?
我正试图找回在我的数据库中注册的所有帐户,以及他们的“个人资料”图像和“封面”图像facebook风格的图像文件路径。映像文件路径存储在与其帐户表不同的表中 我的代码如下:Sql 如何将不同表中的多行合并为一行?,sql,sql-server,tsql,sql-server-2014,Sql,Sql Server,Tsql,Sql Server 2014,我正试图找回在我的数据库中注册的所有帐户,以及他们的“个人资料”图像和“封面”图像facebook风格的图像文件路径。映像文件路径存储在与其帐户表不同的表中 我的代码如下: SELECT a1.AccountID, a1.Forename, a1.Surname, img1.Filename AS ProfileImg, img2.Filename AS CoverImg FROM dbo.Account a1 LEFT JOIN dbo.AccountIma
SELECT
a1.AccountID,
a1.Forename,
a1.Surname,
img1.Filename AS ProfileImg,
img2.Filename AS CoverImg
FROM
dbo.Account a1
LEFT JOIN
dbo.AccountImage img1 ON --table with images in
img1.AccountID = a1.AccountID AND ProfileImg = 1 --boolean column to mark it as profile pic
LEFT JOIN
dbo.AccountImage img2 ON --table with images in
img2.AccountID = a1.AccountID and CoverImg = 1 --boolean column to flag as cover pic
科目表:
AccountID | Forename | Surname |
12345 | Ben | Hur |
12346 | Frodo | Baggins |
12349 | Bill | Gates |
AccountImage表:
AccountID | Filename | ProfileImg | CoverImg
12345 | face.jpg | True | NULL
12345 | bg.jpg | NULL | True
12346 | graph.png | NULL | NULL
12349 | sunset.jpg | NULL | True
当前返回:
|AccountID|Forename|Surname|ProfileImg|CoverImg |
| 12345| Ben| Hur|NULL |bg.jpg |
| 12345| Ben| Hur|face.jpg |NULL |
我需要的结果
|AccountID|Forename|Surname|ProfileImg|CoverImg |
| 12345| Ben| Hur|face.jpg |bg.jpg |
我只想为每个AccountID返回一行,其中分别包含它们的帐户详细信息和图像路径。有数百个帐户。否则我会得到重复的行——一行包含它们的ProfileImg文件路径,另一行包含它们的CoverImg文件路径
请注意,他们可能没有个人资料或封面图片。这意味着两列都可以为空。试试这个
SELECT
a1.AccountID,
a1.Forename,
a1.Surname,
max(img1.Filename) AS ProfileImg,
max(img2.Filename) AS CoverImg
FROM
dbo.Account a1
LEFT JOIN
dbo.AccountImage img1 ON --table with images in
img1.AccountID = a1.AccountID AND ProfileImg = 1 --boolean column to mark it as profile pic
LEFT JOIN
dbo.AccountImage img2 ON --table with images in
img2.AccountID = a1.AccountID and CoverImg = 2
GROUP BY
a1.AccountID,
a1.Forename,
a1.Surname
试试这个
SELECT
a1.AccountID,
a1.Forename,
a1.Surname,
max(img1.Filename) AS ProfileImg,
max(img2.Filename) AS CoverImg
FROM
dbo.Account a1
LEFT JOIN
dbo.AccountImage img1 ON --table with images in
img1.AccountID = a1.AccountID AND ProfileImg = 1 --boolean column to mark it as profile pic
LEFT JOIN
dbo.AccountImage img2 ON --table with images in
img2.AccountID = a1.AccountID and CoverImg = 2
GROUP BY
a1.AccountID,
a1.Forename,
a1.Surname
只是你必须使用内部连接而不是左连接
SELECT
a1.AccountID,
a1.Forename,
a1.Surname,
img1.Filename AS ProfileImg,
img2.Filename AS CoverImg
FROM
dbo.Account a1
INNER JOIN
dbo.AccountImage img1 ON
img1.AccountID = a1.AccountID AND ProfileImg = 1
INNER JOIN
dbo.AccountImage img2 ON
img2.AccountID = a1.AccountID and CoverImg = 2
只是你必须使用内部连接而不是左连接
SELECT
a1.AccountID,
a1.Forename,
a1.Surname,
img1.Filename AS ProfileImg,
img2.Filename AS CoverImg
FROM
dbo.Account a1
INNER JOIN
dbo.AccountImage img1 ON
img1.AccountID = a1.AccountID AND ProfileImg = 1
INNER JOIN
dbo.AccountImage img2 ON
img2.AccountID = a1.AccountID and CoverImg = 2
这里有一个视图可以做到这一点。您需要确保在帐户映像表上设置了唯一的索引,以防止在特定帐户上设置每种类型的多个映像
SELECT AccountID, Forename, Surname,
(SELECT TOP (1) Filename
FROM dbo.AccountImage
WHERE (AccountID = dbo.Account.AccountID) AND (ProfileImg <> 0)) AS ProfileImg,
(SELECT TOP (1) Filename
FROM dbo.AccountImage AS AccountImage_2
WHERE (AccountID = dbo.Account.AccountID) AND (CoverImg <> 0)) AS CoverImg
FROM dbo.Account
WHERE ((SELECT COUNT(*) AS Expr1
FROM dbo.AccountImage AS AccountImage_1
WHERE (AccountID = dbo.Account.AccountID) AND (ProfileImg <> 0) OR
(AccountID = dbo.Account.AccountID) AND (CoverImg <> 0)) = 2)
这里有一个视图可以做到这一点。您需要确保在帐户映像表上设置了唯一的索引,以防止在特定帐户上设置每种类型的多个映像
SELECT AccountID, Forename, Surname,
(SELECT TOP (1) Filename
FROM dbo.AccountImage
WHERE (AccountID = dbo.Account.AccountID) AND (ProfileImg <> 0)) AS ProfileImg,
(SELECT TOP (1) Filename
FROM dbo.AccountImage AS AccountImage_2
WHERE (AccountID = dbo.Account.AccountID) AND (CoverImg <> 0)) AS CoverImg
FROM dbo.Account
WHERE ((SELECT COUNT(*) AS Expr1
FROM dbo.AccountImage AS AccountImage_1
WHERE (AccountID = dbo.Account.AccountID) AND (ProfileImg <> 0) OR
(AccountID = dbo.Account.AccountID) AND (CoverImg <> 0)) = 2)
你能使用MAXimg1.Filename、MAXimg2.Filename和GroupBy吗?我不知道它是否有效,但你尝试过GroupBy吗?我认为它应该有效,只要AccountID、ForName和姓氏相同。。。分组将起作用。请给出这3个表的一些示例数据。它不应该是您给我们的当前报税表,除非AccountImg获得了类似于ProfileImg=1的2行数据。其中1个文件名为null,另一个文件名为/filepath,我认为这不会造成任何影响sense@Raffaello.D.Huke我已经编辑了这个问题,将帐户和AccountImage表的示例数据包括在内。你能使用MAXimg1.Filename、MAXimg2.Filename和Group by吗?我不知道它是否有效,但你试过分组吗?我想应该可以,只要帐号、名字和姓氏相同。。。分组将起作用。请给出这3个表的一些示例数据。它不应该是您给我们的当前报税表,除非AccountImg获得了类似于ProfileImg=1的2行数据。其中1个文件名为null,另一个文件名为/filepath,我认为这不会造成任何影响sense@Raffaello.D.Huke我已经编辑了这个问题,将帐户和AccountImage表的示例数据包括在内,效果非常好,谢谢:。我更新的原始代码中有一个输入错误。它应该是CoverImg=1,因为它是一个布尔字段。工作非常好,谢谢:。我更新的原始代码中有一个输入错误。它应该是CoverImg=1,因为它是一个布尔字段。