2010-09-21 115 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