显示歌手唱歌的所有歌曲列表' Lungi Dance'没有子查询

时间:2018-02-11 05:23:42

标签: sql sql-server

嗨,我想找出由唱歌的歌手演唱的歌曲列表' lungi dance'歌曲。没有子查询就可以找到它。在下面的代码中,我能够显示属于歌手的歌曲名称,但是如何在不使用子查询的情况下通过它的歌曲名称找到歌手名称。

SELECT SongTitle
FROM Singer
INNER JOIN Songs ON Songs.Singer_id=Singer.Singer_id
WHERE Singer_Name='Bohemia'

2 个答案:

答案 0 :(得分:2)

是的,没有子查询(但是第二次加入)是可能的:

SELECT songsOut.SongsTitle
FROM Songs songsCompare
JOIN Singer singer ON (singer.id = songsCompare.Singer_id)
JOIN Songs songsOut ON (songsOut.Singer_id = singer.Singer_id)
WHERE lower(songsCompare.SongTitle) = 'lungi dance';

答案 1 :(得分:2)

标题中的问题:

  

显示歌曲'Lungi'的歌手的所有歌曲列表   舞“

SELECT s3.SongTitle
FROM Singer s1
INNER JOIN Songs s2 ON s1.Singer_id = s2.Singer_id
INNER JOIN Songs s3 ON s2.Singer_id = s3.Singer_id
WHERE s2.SongTitle = 'lungi dance'

enter image description here

[DEMO]

原帖附近的问题:

  

如何在没有使用的情况下通过它的歌曲名称找到歌手的名字   子查询

您只需更改WHERE

中的搜索条件即可
SELECT s1.Singer_name, s2.SongTitle
FROM Singer s1
INNER JOIN Songs s2 ON s1.Singer_id = s2.Singer_id
WHERE s2.SongTitle = 'lungi dance'

enter image description here

示例:

;WITH Singer (Singer_id, Singer_name) AS (
    SELECT 1, 'Bohemia' UNION ALL
    SELECT 2, 'Joe' UNION ALL
    SELECT 3, 'Sam')
, Songs (SongTitle, Singer_id) AS (
    SELECT 'lungi dance', 1 UNION ALL
    SELECT 'Joes Song', 2 UNION ALL
    SELECT 'Sams Song', 3 UNION ALL
    SELECT 'lungi love', 1 UNION ALL
    SELECT 'Joes Hit', 2)

SELECT s1.Singer_name, s2.SongTitle
FROM Singer s1
INNER JOIN Songs s2 ON s1.Singer_id = s2.Singer_id
WHERE s2.SongTitle = 'lungi dance'

现在您可以搜索多首歌曲,并返回歌手:

SELECT s1.Singer_name, s2.SongTitle
FROM Singer s1
INNER JOIN Songs s2 ON s1.Singer_id = s2.Singer_id
WHERE s2.SongTitle IN ('Joes Song', 'Joes Hit', 'lungi dance')

enter image description here