我有5个访问权限的表,分别是IDTable,ArtistTable,BandTable,AlbumTable和SongTable。
我应按艺术家ID搜索,插入,删除和更新。首先,我尝试将ID组合在一起,例如将ID相互依赖;
-------------------- BandTable ----------------------
BandID (PK) BandName
1 Guns and Roses
2 Mumford and Sons
------------------- ArtistTable ----------------------
ArtistID (PK) BandID ArtistName BirthDate City Country
1 2 Marcus Mumford 31.01.1987 London UK
2 2 Winston Marshall
3 2 Ben Lovett
4 2 Ted Dwane
-------------------- AlbumTable ---------------------
AlbumID (PK) ArtistID AlbumName Year Genre AlbumImage
1 1 Sigh No More 2009 Folk Rock
2 1 Babel
-------------------- SongTable ----------------------
SongID (PK) AlbumID SongName Duration Lyricist
1 1 Little Lion Man 4.06 Mumford and Sons
2 1 Winter Winds
3 2 I Will Wait
4 2 Lover of the Light
然而,每个乐队都有艺术家,但并非每个艺术家都有乐队,所以我不能在这里做出加入声明。
然后我尝试将IDTable中的所有ID组合在一起,如下所示;
--------------------- IDTable ------------------------
ID (PK) ArtistID BandID AlbumID SongID
1 1 2 1 1
2 1 2 1 2
3 1 2 2 3
4 1 2 2 4
5 2 2 1 1
6 2 2 1 2
7 2 2 2 3
8 2 2 2 4
------------------- ArtistTable ----------------------
ArtistID (PK) ArtistName BirthDate City Country
1 Marcus Mumford 31.01.1987 London UK
2 Winston Marshall
3 Ben Lovett
4 Ted Dwane
-------------------- BandTable ----------------------
BandID (PK) BandName
1 Guns and Roses
2 Mumford and Sons
-------------------- AlbumTable ---------------------
AlbumID (PK) AlbumName Year Genre AlbumImage
1 Sigh No More 2009 Folk Rock
2 Babel
-------------------- SongTable ----------------------
SongID (PK) SongName Duration Lyricist
1 Little Lion Man 4.06 Mumford and Sons
2 Winter Winds
3 I Will Wait
4 Lover of the Light
(PK)表示主键。
代码是:
String query = "SELECT "
+ " ArtistTable.ArtistName"
+ " ArtistTable.BirthDate"
+ " ArtistTable.City"
+ " ArtistTable.Country"
+ " BandTable.BandName,"
+ " AlbumTable.AlbumName"
+ " AlbumTable.Year"
+ " AlbumTable.Genre"
+ " SongTable.SongName"
+ " SongTable.Duration"
+ " SongTable.Lyricist"
+ "FROM (((IDTable "
+ "INNER JOIN ArtistTable ON IDTable.ArtistID = ArtistTable.ArtistID) "
+ "INNER JOIN BandTable ON IDTable.BandID = BandTable.BandID) "
+ "INNER JOIN AlbumTable ON IDTable.AlbumID = AlbumTable.AlbumID) "
+ "INNER JOIN SongTable ON IDTable.SongID = SongTable.SongID "
+ "WHERE IDTable.ID=" +ID+";";
但它没有用。我不知道是否给出了主键错误或代码错误。
谢谢。
答案 0 :(得分:1)
试试这个:
String query = "SELECT "
+ " ArtistTable.ArtistName,"
+ " ArtistTable.BirthDate,"
+ " ArtistTable.City,"
+ " ArtistTable.Country,"
+ " BandTable.BandName,"
+ " AlbumTable.AlbumName,"
+ " AlbumTable.Year,"
+ " AlbumTable.Genre,"
+ " SongTable.SongName,"
+ " SongTable.Duration,"
+ " SongTable.Lyricist"
+ "FROM IDTable "
+ "INNER JOIN ArtistTable ON IDTable.ArtistID = ArtistTable.ArtistID "
+ "INNER JOIN BandTable ON IDTable.BandID = BandTable.BandID "
+ "INNER JOIN AlbumTable ON IDTable.AlbumID = AlbumTable.AlbumID "
+ "INNER JOIN SongTable ON IDTable.SongID = SongTable.SongID "
+ "WHERE IDTable.ID=" +ID+";";