mysql查询2个表并计数

发布于 2024-11-17 04:06:33 字数 777 浏览 1 评论 0原文

您好,我有来自本地书店的以下查询/表,

$queryadmin ="SELECT last_name, first_name, user_id FROM users";
$recordz = @mysqli_query ($dbc, $queryadmin);

while ($row = mysqli_fetch_array($recordz, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['user_id'] . '</td>
</tr>'
;} 

现在我想要另一个表中的附加列,其中显示每个用户借出的书籍数量。 因此,查询如果单独嵌套会像这样将此

$query2 = mysql_query("SELECT FROM mirror3 WHERE userid='".$row['user_id']."'", $link); 
$anzahl = mysql_num_rows($query2);  

查询嵌套在上面的 while 查询中(就在启动之后),这是行不通的。怎么做呢?

提供的参数不是有效的 MySQL 结果资源

谢谢

Hi i have the following query/table from a local bookstore

$queryadmin ="SELECT last_name, first_name, user_id FROM users";
$recordz = @mysqli_query ($dbc, $queryadmin);

while ($row = mysqli_fetch_array($recordz, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['user_id'] . '</td>
</tr>'
;} 

now i want an additional column from another table where the numbers of books each user has lend out are displayed.
So the query if nested seperately would go something like this

$query2 = mysql_query("SELECT FROM mirror3 WHERE userid='".$row['user_id']."'", $link); 
$anzahl = mysql_num_rows($query2);  

placing this query nested inside the while query (right after while starting) from above does not work. How to do that?

supplied argument is not a valid MySQL
result resource

thanks

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

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

发布评论

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

评论(2

勿忘初心 2024-11-24 04:06:33

你的查询是错误的。指定字段名称:

$query2 = mysql_query("选择
某物来自mirror3,其中userid='".$row['user_id']."'", $link);

Your query is wrong. Specify a field name:

$query2 = mysql_query("SELECT
SOMETHING FROM mirror3 WHERE userid='".$row['user_id']."'", $link);

苍风燃霜 2024-11-24 04:06:33

您可能可以在一个查询中执行此操作:

$query = mysqli_query('SELECT u.last_name, u.first_name, u.user_id, m.PUT_SOMETHING_HERE
        FROM users u
        LEFT JOIN mirror3 m ON u.user_id = m.userid
        WHERE m.PUT_SOMETHING_HERE IS NOT NULL');

但是 Parkyprg 有一个观点,您需要从第二个查询中选择某些内容。

You could probably do this in one query:

$query = mysqli_query('SELECT u.last_name, u.first_name, u.user_id, m.PUT_SOMETHING_HERE
        FROM users u
        LEFT JOIN mirror3 m ON u.user_id = m.userid
        WHERE m.PUT_SOMETHING_HERE IS NOT NULL');

But Parkyprg has a point, you need to be selecting something from that second query.

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