【问题标题】:Using LIKE in a join statement在连接语句中使用 LIKE
【发布时间】:2020-03-11 02:09:14
【问题描述】:

我需要形成一个选择语句,以从我的艺术家表中过滤表格,其中包含所有以字母“Q”开头的艺术家及其各自的歌曲。

艺术家表列:artist_id、name。
歌曲表列:song_id、title、minutes、seconds、genre_id。
song_artist columns(junction): song_id, artist_id;

【问题讨论】:

  • 请向我们展示这两个表格的样本数据和相应的预期结果。
  • 您将在WHERE 子句中JOOIN 之后使用LIKE
  • 请标记您正在使用的数据库引擎,例如SQL Server 或 MySQL。 sql 标签仅适用于他们都使用的语言。
  • @DeanOC - 为什么您认为 MySQL 和 SQL Server 解决方案会有所不同?
  • 关于问题的更新信息

标签: sql string where-clause sql-like


【解决方案1】:

非常基本。

...where artists like 'Q%'...

【讨论】:

    【解决方案2】:

    考虑:

    select a.*, s.*
    from artist a
    inner join song_artist sa on sa.artist_id = a.artist_id
    inner join song s on s.song_id = sa.song_id
    where a.name like 'Q%'
    

    【讨论】:

      【解决方案3】:

      试试这个代码:

      select * from Artists t1
      left outer join Song_Artist t2 on t1.Artist_ID=t2.ArtistID
      left outer join Songs t3 on t2.SongID=t3.Song_Id
      where t1.ArtistName like 'Q%'
      

      【讨论】:

      • 这个涵盖了所有基础,允许匹配的艺术家没有歌曲的情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2015-12-15
      • 2020-01-17
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多