SQL查询多对多关系问题

发布于 2024-11-08 01:14:19 字数 601 浏览 0 评论 0原文

我正在学习 SQL,在构建查询时遇到问题,但找不到任何材料来解决这个问题。我将在专辑/曲目数据库中解释它。

所以这里是

http://imageshack.us/photo/my-images/194/diagramtt.png/

,那是什么我想做:

http://imageshack.us/photo/my-images/803/selectwithwhere.png/

我我确信答案是显而易见的,但这确实让我头疼。

Mariusz


(由 ypercube 编辑):

我的疯狂猜测是 OP 希望显示所有 Albums 和那些 AlbumsTracksno< /strong> 带有 'UnwantedTrackName'Trackname 存在。

I am learning SQL and I have problem with building a query and I can't find any material to solve this. I will explain it on Album/Tracks database.

So here is

http://imageshack.us/photo/my-images/194/diagramtt.png/

and that what I want to do:

http://imageshack.us/photo/my-images/803/selectwithwhere.png/

I am sure answer is obvious but it really gives me a headache.

Mariusz


(EDIT by ypercube):

My wild guess is that OP wants to show all Albums and Tracks for those Albums that no Trackname with 'UnwantedTrackName' exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扮仙女 2024-11-15 01:14:19

试试这个:

select
 a.*
from Album as a
where a.Album_Id not in 
(
    select
     at.Album_Id
    from AlbumTrack as at
        join Track as t
        on t.Track_Id = at.Track_Id
    where t.TrackName ='SomeTrack1'
)

Try this:

select
 a.*
from Album as a
where a.Album_Id not in 
(
    select
     at.Album_Id
    from AlbumTrack as at
        join Track as t
        on t.Track_Id = at.Track_Id
    where t.TrackName ='SomeTrack1'
)
雪花飘飘的天空 2024-11-15 01:14:19

如果您在问题中包含代码和结果,而不是强迫我们重新输入它,对我们来说会容易得多。

您需要所有专辑和曲目,对于不包含某一特定曲目的所有专辑:

select
    a.AlbumName,t.TrackName
from
    Album a
       inner join
    AlbumTrack at
       on
          a.ID = at.AlbumID
       inner join
    Track t
       on
          at.TrackID = t.ID
       left join
    AlbumTrack at_anti
       inner join
    Track t_anti
       on
           at_anti.TrackID = t_anti.TrackID and
           t_anti.TrackName = 'Unwanted Track'
       on
           at.AlbumID = at_anti.AlbumID
where
    at_anti.TrackID is null

Would be a lot easier for us if you included your code and results in your question, rather than forcing us to re-type it.

You want all Albums and Tracks, for all Albums which don't include one particular track:

select
    a.AlbumName,t.TrackName
from
    Album a
       inner join
    AlbumTrack at
       on
          a.ID = at.AlbumID
       inner join
    Track t
       on
          at.TrackID = t.ID
       left join
    AlbumTrack at_anti
       inner join
    Track t_anti
       on
           at_anti.TrackID = t_anti.TrackID and
           t_anti.TrackName = 'Unwanted Track'
       on
           at.AlbumID = at_anti.AlbumID
where
    at_anti.TrackID is null
遇见了你 2024-11-15 01:14:19

不太清楚你在问什么,但是“INNER JOIN”会给你你想要的吗?

It's not quite clear what you are asking, but will "INNER JOIN" give you what you want?

月依秋水 2024-11-15 01:14:19

尝试对轨道表进行内部连接。如果这不起作用,请将 WHERE 条件推入 ON 子句 - 就像

track AS t ON (t.id = at.TrackID AND t.TrackName != 'UnwantedTrackName')

Try INNER JOINing the tracks table. If that doesn't work, push the WHERE condition into the ON clause - like

track AS t ON (t.id = at.TrackID AND t.TrackName != 'UnwantedTrackName')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文