MySQL找到一行的副本并放入一个字段

时间:2014-04-27 07:15:51

标签: mysql

我有这张桌子: enter image description here

通过此查询,我提取未激活的歌曲,但我想添加一个值(重复)如果歌曲有激活的重复,我该怎么办?

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,
  IF(CONDITION, 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'
;

感谢。

修改
我希望得到这个:

song_id|song_name|artist_id|artist_name|description|status|activated|giftable|duplicate
1      | "name"  | "id"    | "name"    | "descr"   | 0    | 0       | 1      | 1  
2      | "name"  | "id"    | "name"    | "descr"   | 1    | 0       | 0      | 0

1 个答案:

答案 0 :(得分:0)

试试:

select song_id , artist_id,status,activated,max(duplicate) duplicate from(

SELECT 
song_id,

artist_id,

status,
activated,
giftable,
if(activated = 1,1 ,0) duplicate
FROM Table1
WHERE


artist_id = '1'

)t
group by song_id

DEMO HERE