SELECT 仅限于基于单独表中的 CSV 列表的结果

发布于 2024-08-20 10:37:52 字数 600 浏览 6 评论 0原文

所以我在一个表中有一个 CSV 列表。 (例如:1,3,19) 我想从另一个表中搜索 id 与其中任何一个匹配的所有用户名。

我觉得我应该能够做类似的事情:

<?
$query = "SELECT player_ids FROM cast_list WHERE game='".$gameid."' ";
$result = mysql_query($query) or die(mysql_error());
$playerquery = "SELECT username,id FROM players WHERE id IN (".$result.") ORDER BY username;
$player_result = mysql_query($playerquery) or die(mysql_error());
echo "<ul>";
while ($row = mysql_fetch_array($player_result) ) {
  echo "<li>".$row['username']."</li>";
}
echo "</ul>";
?>

但我无法让它发挥作用。我做错了什么?

So I have a list of CSVs in one table. (EG: 1,3,19 )
I want to search out all of the usernames from the other table where the ids match any of those.

I feel like I should be able to do something like:

<?
$query = "SELECT player_ids FROM cast_list WHERE game='".$gameid."' ";
$result = mysql_query($query) or die(mysql_error());
$playerquery = "SELECT username,id FROM players WHERE id IN (".$result.") ORDER BY username;
$player_result = mysql_query($playerquery) or die(mysql_error());
echo "<ul>";
while ($row = mysql_fetch_array($player_result) ) {
  echo "<li>".$row['username']."</li>";
}
echo "</ul>";
?>

but I can't get it to work. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风轻花落早 2024-08-27 10:37:52

您还可以使用 子查询 (这会更快):

$playerquery = "SELECT username,id 
                FROM players 
                WHERE id IN (SELECT player_ids FROM cast_list WHERE game='".$gameid."') 
                ORDER BY username";

顺便说一句,如果 game 是一个整数字段,则您无需在值周围添加引号 (' ')。

You can also use a subquery (which will be faster):

$playerquery = "SELECT username,id 
                FROM players 
                WHERE id IN (SELECT player_ids FROM cast_list WHERE game='".$gameid."') 
                ORDER BY username";

Btw if game is an integer field you don't have put quotes (' ') around the value.

念﹏祤嫣 2024-08-27 10:37:52

这个想法是正确的,但您需要将 $result 传输到实际的字符串数组:

$game_ids = array();
while ($row = mysql_fetch_array($result) ) {
 $game_ids[] = .$row[1];
}

现在使用 implode 将数组转换为用逗号分隔的值:

$playerquery = "SELECT username,id FROM players WHERE id IN (" . implode(",",$result) . ") ORDER BY username;

The idea is correct, but you need to transfer the $result to an actual string array:

$game_ids = array();
while ($row = mysql_fetch_array($result) ) {
 $game_ids[] = .$row[1];
}

Now using implode to convert the array to a comma separated values with a comma:

$playerquery = "SELECT username,id FROM players WHERE id IN (" . implode(",",$result) . ") ORDER BY username;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文