我有这个问题:
SET @id := '';
SELECT
libs.song_id,
songs.name AS song_name,
artists.id AS artist_id,
artists.name AS artist_name,
songs.description,
libs.status,
libs.activated,
libs.giftable,
@id := libs.song_id,
IF((SELECT activated FROM libs WHERE @id = song_id && activated = 1), 1, 0) AS duplicate
FROM libs, songs, artists
WHERE
libs.song_id = songs.id &&
artists.id = songs.artist_id &&
libs.activated = 0 &&
libs.user_id = '1';
一切都按计划进行,但我还得到一个名为@id := libs.song_id
的专栏,我该如何避免呢?
感谢。
答案 0 :(得分:1)
首先要做的一些事情。学习使用正确的连接语法。一个简单的规则:只是不要在from
子句中使用逗号。
其次,您甚至不必将变量初始化为单独的语句。
第三,这个想法很简单。只需在另一个表达式中分配变量,就有一个非常自然的地方:
SELECT (@id := libs.song_id) as song_id,
songs.name AS song_name, artists.id AS artist_id, artists.name AS artist_name,
songs.description, libs.status, libs.activated, libs.giftable,
IF((SELECT activated FROM libs WHERE @id = song_id AND activated = 1), 1, 0) AS duplicate
FROM libs join
songs
on libs.song_id = songs.id join
artists
on artists.id = songs.artist_id
WHERE libs.activated = 0 AND libs.user_id = '1';