Mysql 使用 find_in_set 和两个字符串列表进行选择
我的表中有这样 2 行:
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
我需要进行一个按类型排序的选择查询,因此 FIFA 11 将排在第一,而 halo:reach 第二。所以我尝试使用 find_in_set 但它不适用于两个字符串列表。
我想到的唯一选择是对每个流派执行 IN 子句并按某些字段排序并进行限制,但我认为这样做确实很糟糕。
I got in my table this 2 rows:
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', '', ''
And in my php script I got:
$Genres = "sci-fi,sport,soccer"; // After using the sumbit button
I need to make a select query that order by genres, so FIFA 11 would be first and halo:reach second. So I tried to use find_in_set but it not working with two string list.
The only option I think about was to do for each genre IN clause and order by some fields and making limit, but I think its really bad way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IN 子句是要走的路。
只需通过在 PHP 中引用/转义它来准备流派数组,然后用逗号分隔符将其内爆即可。
SELECT * FROM tbl_games WHERE 类型 IN ('科幻', '运动'...) ORDER BY 类型
IN clause is the way to go.
Just prepare the genres array by quoting/escaping it in PHP and then implode it with comma delimiter.
SELECT * FROM tbl_games WHERE genre IN ('sci-fi', 'sport'...) ORDER BY genre
我找到了更好的方法:D
我创建了一个新表(游戏类型),因此对于每个游戏,我在那里都有一些行。
I found better way :D
I created a new table (gamesgenres), so for every game I got some rows there.