SELECT 仅限于基于单独表中的 CSV 列表的结果
所以我在一个表中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用 子查询 (这会更快):
顺便说一句,如果
game
是一个整数字段,则您无需在值周围添加引号 ('
'
)。You can also use a subquery (which will be faster):
Btw if
game
is an integer field you don't have put quotes ('
'
) around the value.这个想法是正确的,但您需要将
$result
传输到实际的字符串数组:现在使用
implode
将数组转换为用逗号分隔的值:The idea is correct, but you need to transfer the
$result
to an actual string array:Now using
implode
to convert the array to a comma separated values with a comma: