我正在尝试根据使用以下查询的 id 集合从多个表中检索记录,它工作正常。
活动记录查询:
Song.order("songs.id desc"). 包括(:艺术家)。包括(:专辑)。joins("LEFT JOIN 艺术家 ON song.artist_id = Artists.id"). joins("LEFT JOIN 专辑 ON song.album_id = albums.id"). 在哪里(“(歌曲.id)
IN (#{song_ids*','})
AND (LOWER(songs.title) LIKE :term OR LOWER(artists.name) LIKE :term OR LOWER(albums.title) LIKE :term)", :term => "%#{terms[index].downcase}% ")
PG查询输出:
SELECT "songs".* FROM "songs" LEFT JOIN 艺术家 ON song.artist_id = Artist.id LEFT JOIN 专辑 ON song.album_id = albums.id WHERE (
(songs.id) IN (4,5,28,37,46,48)
AND (LOWER(songs.title) LIKE '%wo%' OR LOWER(artists.name) LIKE '%wo%' OR LOWER(albums.title) LIKE '%wo%')) ORDER BY song.id desc
但是,当我尝试将查询修改为以下活动
记录查询时:
Song.order("songs.id desc"). 包括(:艺术家)。包括(:专辑)。joins("LEFT JOIN 艺术家 ON song.artist_id = Artists.id"). joins("LEFT JOIN 专辑 ON song.album_id = albums.id"). 在哪里(“(歌曲.id)
在 (:song_ids)
AND (LOWER(songs.title) LIKE :term OR LOWER(artists.name) LIKE :term OR LOWER(albums.title) LIKE :term)",
:term => "%#{terms[index].downcase}%",:song_ids => song_ids*',')
PG查询输出:
SELECT "songs".* FROM "songs" LEFT JOIN 艺术家 ON song.artist_id = Artist.id LEFT JOIN 专辑 ON song.album_id = albums.id WHERE (
(songs.id) IN ('4,5,28,37,46,48')
AND (LOWER(songs.title) LIKE '%wo%' OR LOWER(artists.name) LIKE '%wo%' OR LOWER(albums.title) LIKE '%wo%')) ORDER BY song.id desc
由于单引号,上述查询不起作用。