SELECT `osid`,`os_name`,
(SELECT count(*)
FROM `game_game`
WHERE concat(',',`os`,',') LIKE ('%,`os`.`osid`,%') ) as game_count
FROM `game_os` AS os ORDER BY `osid`
两个SQL表
game_os
osid (PK)
os_name
game_game
game_id (PK)
game_name
os (save data format : 1,2,3) (1,2,3 = osid)
这个SQL查询,game_count从不= 0
我觉得可能是'%,os
。osid
,%'因为'中的os
。osid
XX' < -
我该如何解决?
我需要在某个操作系统中获得多少游戏
答案 0 :(得分:3)
concat()
:{/ p>需要like
SELECT osid, os_name,
(SELECT count(*)
FROM game_game
WHERE concat(',', os, ',') LIKE CONCAT('%,', os.osid, ',%')
) as game_count
FROM game_os AS os
ORDER BY osid;
但是,您也可以使用find_in_set()
来编写此内容,因为您使用逗号分隔符:
SELECT osid, os_name,
(SELECT count(*)
FROM game_game
WHERE find_in_set(os.osid, os) > 0
) as game_count
FROM game_os AS os
ORDER BY osid;