php - mysql,1 个具有 where 条件的表与另一个具有不同 where 条件的表连接

标签 php mysql

首先,我对标题中的粗俗术语表示歉意。

<? if ($thisguy == "noob") { echo("bear with me"); } ?>

我有两个表:

一个“评级”(行:产品、用户、评级、已添加) WHERE user = $thisuser[id],

和一个“产品”(id、名称),其中 product.id = rating.product

我广泛阅读了有关连接(内连接、外连接、左连接、右连接...)的内容,但我对此很困惑。

我需要数组中 Product.id = ratings.product 和 user = $thisuser[id] 的所有信息。

我正在使用这个:

$result = mysql_query("SELECT ratings.product, ratings.user, ratings.rating, ratings.added FROM ratings INNER JOIN products ON products.id = ratings.product AND user = $THISUSER[id]");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $product = $row['product'];
    $user = $row['user'];
    $rating = $row['rating'];
    $added = $row['added'];
    $pid = $row['id'];
    $pname = $row['name'];
    }
print("$user, $rating, $added, $THISUSER[id], $pname, $pid"); }

这会产生评级表中的所有内容(但只有一个实例),而不会产生产品表中的任何内容...我已经尝试了 100 种不同的方法,但均无济于事。任何帮助将不胜感激!对我轻松一些。 *畏缩

最佳答案

您没有从产品表中选择任何列,请更改您的选择

$result = mysql_query("SELECT ratings.product, ratings.user, ratings.rating, ratings.added, products.id, products.name FROM ratings INNER JOIN products ON products.id = ratings.product where ratings.user = $THISUSER[id]");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $product = $row['product'];
    $user = $row['user'];
    $rating = $row['rating'];
    $added = $row['added'];
    $pid = $row['id'];
    $pname = $row['name'];
    }
print("$user, $rating, $added, $THISUSER[id], $pname, $pid"); }

关于php - mysql,1 个具有 where 条件的表与另一个具有不同 where 条件的表连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684042/

相关文章:

php - PHP 的评论系统

php - Paypal IPN 响应适用于 Paypal 但不适用于 sandbox-Paypal?

mysql - MySQL 中的表为空集

php - 从 MySQL 中选择行

php - MAMP 上的 ExpressionEngine

php - 使用 php 将 excel 表导入 mysql 表

php - scanimage 和 shell_exec ...可能吗?

php - SQLite:对特定查询执行区分大小写的 LIKE 查询

java - Google Mirror API 中的相机访问

mysql - 如何使用单个查询在单个表中编写多个查询