从不同表SQL Server中的乘法行中选择一行中的数据

时间:2018-01-23 18:04:09

标签: sql sql-server

我在这里寻找答案,但找不到我的问题的正确答案。我有桌子:

----------    ----------    ----------
| offers |    |  bids  |    | users  |
----------    ----------    ----------
id            id            id
....          offer_id      first_name
....          user_id       last_name
              ....          .....

现在我需要为每个商品ID分配用户的姓氏和名字

offers.id = bids.offers_id and bids.user_id = users.id

示例:

     offers                   bids                   users
------------------    ----------------------     ----------------
|id| ...| ....|       |id| offer_id| user_id|    |id| first_name| last_name|
------------------    -----------------------     --------------------------
|1 | ...|.....|       |1 |    1    |    2   |    | 2| Peter     | Jackson  |
|2 | ...|.....|       |2 |    1    |    3   |    | 3| Adam      | Black    |
                      |3 |    1    |    6   |    | 4| Roy       | Wright   |
                      |4 |    2    |    5   |    | 5| Eva       | Cekovsky |
                      |5 |    2    |    7   |    | 6| Martin    | Tyson    |
                                                 | 7| Vera      | Vornel   |

输出应该是这样的

  offer_id                   full_name
--------------------------------------------------------------
|   1      |    Peter Jackson, Adam Black, Martin Tyson      |
--------------------------------------------------------------
|   2      |    Eva Cekowsky, Vera Vornel                    |
--------------------------------------------------------------

我可以使用CONCAT轻松地使用名字连接姓氏,如果每个数据都在同一个表中,那么这样做,但是,我无法弄明白。

2 个答案:

答案 0 :(得分:3)

对于MS SQL Server 2017,您可以使用STRING_AGG函数:

SELECT
    b.offer_id,
    STRING_AGG (CONCAT(u.first_name,' ',u.last_name), ', ')
        WITHIN GROUP (ORDER BY u.last_name, u.first_name ASC)
        AS full_name
FROM bids b
JOIN users u on b.user_id = u.id
GROUP BY b.offer_id

您可以强制使用逗号分隔的名称,例如:

Sub aMacro()
'
' aMacro Macro
'
'
Dim t As Table
    Dim doc As Word.Document
    For Each t In ActiveDocument.Tables
        Dim Cell As String
        Cell = t.Cell(1, 1).Range.Text
        If StrComp(Cell, "Name") Then 'This one probably does as well but I haven't tested it yet
            Dim Name As String
            Dim Amount As String
            Dim Value As String
            Dim valueInt As Integer
                Name = t.Cell(2, 1).Range.Text
                Value = t.Cell(2, 3).Range.Text
                Amount = t.Cell(2, 4).Range.Text
                valueInt = 0
                If StrComp(Value, "Monday") Then 'this always activates, regardless of what I compare
                    valueInt = 5
                    Selection.TypeText Text:="This is here"                     
                    ElseIf StrComp(Value, "Tuesday") Then
                    valueInt = 4
                End If
            Selection.TypeText Text:="Name: " + Name 'These all print out with the correct values from the supplied tables
            Selection.TypeText Text:="Amount: " + Amount
            Selection.TypeText Text:="Value: " + Value                    
            Selection.TypeText Text:=valueInt
        End If
    Next
End Sub

答案 1 :(得分:1)

这应该是你所追求的。它使用FOR XML PATHSTUFF创建分隔字符串:

CREATE TABLE #offer (id int);
INSERT INTO #offer
VALUES (1),(2);

CREATE TABLE #bid (id int,
                   offer_id int,
                   user_id int);
INSERT INTO #bid
VALUES (1,1,2),
       (2,1,3),
       (3,1,6),
       (4,2,5),
       (5,2,7);

CREATE TABLE #user (id int,
                    first_name varchar(10),
                    last_name varchar(10));
INSERT INTO #user
VALUES (2,'Peter','Jackson'),
       (3,'Adam','Black'),
       (4,'Roy','Wright'),
       (5,'Eva','Cekovsky'),
       (6,'Martin','Tyson'),
       (7,'Vera','Vornel');
GO
SELECT o.id AS offer_id,
       STUFF((SELECT ', ' + first_name + ' ' + last_name
              FROM #bid b
                   JOIN #user u ON b.user_id = u.id
              WHERE b.offer_id = o.id
              FOR XML PATH('')),1,2,'') AS Full_name
FROM #offer o;

GO
DROP TABLE #offer;
DROP TABLE #bid;
DROP TABLE #user;

如果您有任何疑问,请发表评论。