2010-09-21 117 views
0

我在我的表得到这个2行:Mysql的选择与FIND_IN_SET两个StringList的

1, 'Halo: Reach', 2010, ''fps','sci-fi'', '"The best game of the year".', 'Microsoft', 'Bungie', 'XBOX 360', 9.5, 'http://wuwb.com/wp/wp-content/uploads/2009/06/halo_reach.thumbnail.jpg', '', '' 

2, 'FIFA 11', 2010, 'sport,soccer', '"The best soccer game ever"', 'EA', 'EA', 'PC, XBOX 360, PS3', 10, 'http://wuwb.com/wp/wp-content/uploads/2009/06/halo_reach.thumbnail.jpg', '', '' 

而且在我的PHP脚本我有:

$Genres = "sci-fi,sport,soccer"; // After using the sumbit button 

我需要做的顺序选择查询按类型划分,所以国际足联11将首先和光环:达到第二。所以我尝试使用find_in_set,但它不能使用两个字符串列表。

我想到的唯一选择是为每个类型的IN子句和一些字段的排序和限制做的事情,但我认为这是非常糟糕的方式来做到这一点。

回答

0

我找到更好的办法:d

我创建了一个新表(gamesgenres),所以每场比赛我得到了一些行存在。

SELECT count(games.id) as relevance, GROUP_CONCAT(gamesgenres.genre) as genres, games.* FROM games, gamesgenres WHERE ('sci-fi' IN (gamesgenres.genre) OR 'soccer' IN (gamesgenres.genre) OR 'fps' IN (gamesgenres.genre)) AND gamesgenres.game = games.genres group by games.id ORDER BY relevance DESC; 
0

IN子句是要走的路。 只需通过在PHP中引用/转义来准备流派数组,然后用逗号分隔符对其进行分解。

SELECT * FROM tbl_games WHERE genre IN ('sci-fi', 'sport'...) ORDER BY genre