首先,我对标题中粗略的术语表示道歉。
<? if ($thisguy == "noob") { echo("bear with me"); } ?>
我有两张桌子:
一个&#34; 评分&#34; (行:产品,用户,评级,已添加)WHERE user = $thisuser[id]
,
和一个&#34; 产品&#34; (id,name)WHERE product.id = rating.product
。
我已经广泛阅读了关于联盟(内部,外部,左侧,右侧......)的信息,而且我对此感到困惑。
我需要数组中的所有信息WHERE Product.id = ratings.product and 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种不同的方法无效。任何帮助将不胜感激!放轻松我。 *畏缩
答案 0 :(得分:1)
您没有从产品表中选择任何列,更改您的选择
$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"); }
答案 1 :(得分:0)
SELECT ratings.product, ratings.user, ratings.rating, ratings.added FROM ratings INNER JOIN products ON products.id = ratings.product WHERE ratings.user = $THISUSER[id]
试试这个,在最后一个句子中使用WHERE而不是AND。
答案 2 :(得分:0)
确保您在查询中提供的数据库名称是正确的,因为您的描述包含的表名与您所使用的代码不同。
注意:您仅选择了评级表而没有从产品表中选择,在我的代码中我从产品表中选择了产品名称。
您必须使用ON
条件以及WHERE
,以便在使用JOIN query
时完美无缺。由于Join QUERY的语法是这样的。
您可以在WHERE中的ON和check值中提供比较值,以便您的数据看起来与您期望的一致。
JOIN QUERY
在您的情况下将如下所示。
$query = "SELECT rating.product,rating.user,rating.rating,product.name FROM rating INNER JOIN products ON products.id = rating.product WHERE rating.user ='".$thisuser['id']."'";
答案 3 :(得分:0)
确保$ THISUSER [id]包含有效值。
尝试以这种方式分配var
$acc_user = $THISUSER[id];
"SELECT ratings.product, ratings.user, ratings.rating, ratings.added
FROM ratings
INNER JOIN products ON (products.id = ratings.product
AND ratings.user = $acc_user) "
你可以使用on子句并最终放入inside()