MySQL查询中的单引号不适用于concat()

时间:2014-12-05 16:47:04

标签: php sql

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

我觉得可能是'%,ososid,%'因为'中的ososid XX' < -

我该如何解决?

我需要在某个操作系统中获得多少游戏

1 个答案:

答案 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;