如何将带有多个值的WHERE IN子句转换为RegExp或Like

时间:2017-12-14 17:23:09

标签: mysql regex sql-like where-in

我想转换一下:

SELECT id,songTitle,artistName, trackId
FROM songs
WHERE (songTitle, artistName) IN (('come together', 'the beatles'),('all the small things', 'blink-182'))

对于这样的事情,但我不知道正确的语法:

SELECT id,songTitle,artistName, trackId
FROM songs
WHERE (songTitle, artistName) IN LIKE (('%come together%', '%the beatles%'),('%all the small things%', '%blink-182%'))

除了我一次搜索100多首歌曲。我们也可以使用REGEXP,我只是不知道其中任何一个的正确语法。

1 个答案:

答案 0 :(得分:1)

  • WHERE (a,b) IN ((1,2), ...)的优化效果非常差。
  • LIKE中的主要外卡阻止使用索引。
  • 你不能做你想要的构造。

因此,除了性能之外,我们来看看如何执行任务:

WHERE ( songTitle LIKE '%come together%' AND artistName LIKE '%the beatles%')
   OR ( .... )
   OR ...

对不起,没有捷径。

在这种情况下,REGEXP无法帮助

FULLTEXT索引是需要考虑的事情,但我不认为它会对此示例有所帮助。