0

私はこのテーブルを持っています:

映画

| Name | Genre |

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

ジャンル

| Name     | GenreID |

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

映画_ジャンル

| Film | Genre |

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

VB.NET 2010 と .NET FrameWork 2.0 ソース コード:

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

上記の結果をどのように解決できますか?

私はこの結果を得たい:

| Film | Genres                 |

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

助けてくれてありがとう、そして私の悪い英語をごめんなさい!

4

1 に答える 1

3

あなたの質問に対する即時の解決策は次のとおりです。

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

削除したい重複がある可能性がある場合は、使用できますLIST(DISTINCT g.Name)

また、列Films.Genreは本当に間違った名前です。または似たようなことを意味していたと思いますFilms.FilmID

これはかなり基本的な SQL であるため、結合について読むことをお勧めします。

于 2013-03-08T11:53:50.887 に答える