简体   繁体   中英

VB.NET Firebird Results Separated by Commas

I have this tables:

Films

| Name | Genre |

| A    | 1     |
| B    | 2     |
| C    | 3     |
| D    | 4     |
| E    | 5     |

Genres

| Name     | GenreID |

| Action   | 1       |
| Sci-Fi   | 2       |
| Horror   | 3       |
| Comics   | 4       |
| Romantic | 5       |

Film_Genre

| Film | Genre |

| 1    | 2     |
| 1    | 3     |
| 1    | 4     |
| 2    | 1     |
| 2    | 4     |
| 3    | 2     |

VB.NET 2010 with .NET FrameWork 2.0 source code:

Imports FirebirdSql.Data.FirebirdClient
Public Class Form1
Dim sCommand As FbBatchExecution
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    DataGridView1.Columns.Add(0, "Part")
    DataGridView1.Columns.Add(1, "Partn")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connection As New FbConnection("Database=e:\DBSCHOOL.FDB;User=SYSDBA;Password=masterkey;Dialect=3;ServerType=1")
    connection.Open()
    sCommand = New FbBatchExecution(connection)
    AddHandler sCommand.CommandExecuted, AddressOf CmdE

    'sCommand.SqlStatements.Add("CREATE TABLE Films (Name varchar(50), Genre int)")
    'sCommand.SqlStatements.Add("insert into Films (Name, Genre) values ('A', 1);")
    'sCommand.SqlStatements.Add("insert into Films (Name, Genre) values ('B', 2);")
    'sCommand.SqlStatements.Add("insert into Films (Name, Genre) values ('C', 3);")
    'sCommand.SqlStatements.Add("insert into Films (Name, Genre) values ('D', 4);")
    'sCommand.SqlStatements.Add("CREATE TABLE Film_genre (Film varchar(50), Genre int)")
    'sCommand.SqlStatements.Add("insert into Film_genre (Film, Genre) values (1, 2);")
    'sCommand.SqlStatements.Add("insert into Film_genre (Film, Genre) values (1, 3);")
    'sCommand.SqlStatements.Add("insert into Film_genre (Film, Genre) values (1, 4);")
    'sCommand.SqlStatements.Add("insert into Film_genre (Film, Genre) values (2, 1);")
    'sCommand.SqlStatements.Add("insert into Film_genre (Film, Genre) values (2, 4);")
    'sCommand.SqlStatements.Add("insert into Film_genre (Film, Genre) values (3, 2);")
'sCommand.SqlStatements.Add("CREATE TABLE Genres (Name varchar(20), GenreID int)")
'sCommand.SqlStatements.Add("insert into Genres (Name, GenreID) values ('Action', 1);")
'sCommand.SqlStatements.Add("insert into Genres (Name, GenreID) values ('Sci-Fi', 2);")
'sCommand.SqlStatements.Add("insert into Genres (Name, GenreID) values ('Horror', 3);")
'sCommand.SqlStatements.Add("insert into Genres (Name, GenreID) values ('Comics', 4);")
'sCommand.SqlStatements.Add("insert into Genres (Name, GenreID) values ('Romantic', 5);")

    sCommand.SqlStatements.Add("SELECT Name, LIST(Genre,';') from Films group by Name;")

    sCommand.Execute()
    connection.Close()
End Sub
Sub CmdE(ByVal sender As System.Object, ByVal e As CommandExecutedEventArgs)
    If e.StatementType = 63 Then
        DataGridView1.Rows.Clear()
        Dim Table As List(Of DataGridViewRow) = New List(Of DataGridViewRow)
        Dim FBDR As FbDataReader = e.DataReader
        While FBDR.Read()
            Dim adatok(FBDR.FieldCount - 1) As String
            For i = 0 To FBDR.FieldCount - 1
                adatok(i) = FBDR(i).ToString
            Next
            Table.Add(New DataGridViewRow())
            Table(Table.Count - 1).CreateCells(DataGridView1, adatok)
        End While
        DataGridView1.Rows.AddRange(Table.ToArray)
    End If
End Sub
End Class

How can I solve the above-mentioned results we get?

I want to get this result:

| Film | Genres                 |

| A    | Sci-Fi, Horror, Comics |
| B    | Action, Comics         |
| C    | Sci-Fi                 |

Thanks for help, and sorry my bad english!

The immediate solution to your question is:

SELECT f.Name, LIST(g.Name)
FROM Films f
INNER JOIN Film_Genre fg ON fg.Film = f.Genre
INNER JOIN Genres g ON g.GenreID = fg.Genre
GROUP BY f.Name

If there could be duplicates you want removed, you can use LIST(DISTINCT g.Name)

Also the column Films.Genre is really a wrong name. I suppose you meant Films.FilmID or something similar.

As this is rather basic SQL, I suggest you read up on joins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM