艺术家表:
Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL |
歌曲表:
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(80) | YES | | NULL | |
| minutes | int(11) | YES | | NULL | |
| seconds | int(11) | YES | | NULL | |
| sales | decimal(5,2) | NO | | 0.00 | |
| genre_id | int(11) | NO | MUL | NULL |
和我的联结表song_artist:
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| song_id | int(11) | NO | PRI | NULL | |
| artist_id | int(11) | NO | PRI | NULL
因此,我的数据中没有歌手,而数据中也没有歌手。我可以使用左后右外连接使没有歌曲的艺术家出现,但是我似乎无法以相反的方式获取数据。 我一直试图使用此代码来获取表中没有任何歌手的歌曲:
SELECT songs.title, artists.name
-> FROM songs
-> RIGHT JOIN song_artist
-> ON (songs.id = song_artist.song_id)
-> RIGHT JOIN artists
-> ON (artists.id = song_artist.artist_id);
但这只是行不通。我需要所有歌曲的名称和歌手的名字才能显示在我的结果中。
我需要这样的结果:
title | name |
+-----------------------+-------------------------------+
| Under Pressure | Queen |
| Let It Be | Beatles |
| Broadway | Goo Goo Dolls |
| I Hope You Dance | Lee Ann Womack |
| Turn Off The Light | Nelly Furtado |
| I'm Like a Bird | Nelly Furtado |
| Canon in D | Barrymoore Chamber Orchestra |
| Canon in D | London Philharmonic Orchestra |
| Star Wars | London Philharmonic Orchestra |
| Sea Drift | London Philharmonic Orchestra |
| NULL | Pop Evil |
| NULL | Volbeat |
| NULL | Godsmack |
但是我的名字类别而不是标题类别需要NULL
答案 0 :(得分:0)
要获取没有任何艺术家的歌曲,可以使用not exists
:
select s.*
from songs s
where not exists (
select 1
from song_artist sa
inner join artist a on a.id = sa.artist_id
where sa.song_id = s.id
)
这涉及以下情况:
song_artist
中没有给定歌曲的条目song_artist
中有给定歌曲的一个(或多个)条目,但是在表artist_id
中找不到引用的artist
(s)。答案 1 :(得分:0)
在这种情况下,可以使用 import java.util.Scanner;
public class Letter
{
public static void main(String[] args)
{
// Ask the user for 3 things: their word, letter they want to replace,
// and replacing letter.
Scanner input = new Scanner(System.in);
System.out.println("Enter your word:");
String word = input.nextLine();
System.out.println();
System.out.println("Enter the letter you want to replace:");
String letter = input.nextLine();
System.out.println();
System.out.println("Enter the replacing letter:");
String replace = input.nextLine();
System.out.println();
// Call the method replaceLetter and pass all 3 of these items to it for
// string processing.
}
// Modify this method so that it will take a third parameter from a user that is the String
//they
//want
//to replace letterToReplace with. This method should return the modified String.
public static int replaceLetter(String word, String letterToReplace, String replacement)
{
int count = 0;
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i, i+1).equals(letterToReplace))
{
count++;
}
}
return count;
}
}
和LEFT JOIN
子句返回不匹配的行。
对于没有歌手的歌曲:
WHERE
修改
如果您要所有歌曲,包括有的歌曲和没有的有歌手的歌曲,则:
SELECT songs.*
FROM songs LEFT JOIN song_artist
ON song_artist.song_id = songs.id
WHERE song_artist.song_id IS NULL