Mysql 使用 find_in_set 和两个字符串列表进行选择

发布于 2024-09-24 07:37:07 字数 695 浏览 0 评论 0原文

我的表中有这样 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-10-01 07:37:07

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

脸赞 2024-10-01 07:37:07

我找到了更好的方法:D

我创建了一个新表(游戏类型),因此对于每个游戏,我在那里都有一些行。

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;

I found better way :D

I created a new table (gamesgenres), so for every game I got some rows there.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文