我有这张表:
薄膜
| 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
| Film | Genre |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 4 |
| 3 | 2 |
VB.NET 2010 with .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 |
感谢您的帮助,对不起我的英语不好!
答案 0 :(得分: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,我建议你阅读联接。