PHP 中的 mysql 子查询
我有一个 mysql 查询
,它使用子查询
来选择不在 group_member 表中的组 10 中的所有用户。该查询在 MYsql 工作台上运行良好并返回预期结果。然而,当我在 PHP 脚本中运行它时,它并没有运行子查询部分,而是只是返回所有用户的列表。
任何人都知道这里发生了什么,或者可能有一种不需要子查询的方法?
include('config.php');
$server = $cfg->dbhost;
$link = mysql_connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass);
$select = mysql_select_db($cfg->dbname);
$query = "SELECT * FROM usr WHERE usr.id NOT IN (SELECT DISTINCT member FROM mahara.group_member WHERE mahara.group_member.group='10')";
echo '</br>' . $query . '</br>';
$result = mysql_query($query, $link);
while ($row = mysql_fetch_assoc($result)) {
echo $row['id'] , ' ' , $row['username'] , ' ' , $row['firstname'] , ' ' , $row['lastname'] , ' ' , $row['email'] , '</br>';
}
I have a mysql query
that uses a subquery
to select all users that are not in group 10 in the group_member table. the query works fine on MYsql
workbench and brings back the expected results. However when I run it in a PHP script
it's not runnning the subquery part, in stead it's just bringing back a list of all users.
Anyone got any ideas what's going on here or perhaps an approach that doesn't need the subquery?
include('config.php');
$server = $cfg->dbhost;
$link = mysql_connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass);
$select = mysql_select_db($cfg->dbname);
$query = "SELECT * FROM usr WHERE usr.id NOT IN (SELECT DISTINCT member FROM mahara.group_member WHERE mahara.group_member.group='10')";
echo '</br>' . $query . '</br>';
$result = mysql_query($query, $link);
while ($row = mysql_fetch_assoc($result)) {
echo $row['id'] , ' ' , $row['username'] , ' ' , $row['firstname'] , ' ' , $row['lastname'] , ' ' , $row['email'] , '</br>';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论