[英]How do I display all records from a SQL table and match those records to another table?
I have set the PHP variable $accountnumber to be that of the user who is viewing their profile page. 我将PHP变量$ accountnumber设置为正在查看其个人资料页面的用户的变量。 On the page, I have a block with the user's information populated from the database, and I have a list of all products that we have, and I want to put a check mark next to each one that the customer has by assigning a class to it. 在页面上,我有一个块,其中包含从数据库中填充的用户信息,并且我拥有我们拥有的所有产品的列表,并且我想通过为客户分配一个类来在客户拥有的每个产品旁边打一个勾号。它。
Here are my tables: 这是我的桌子:
products
id | name | url | weight
100 p1 p1.html 1
101 p2 p2.html 2
102 p3 p3.html 3
103 p4 p4.html 4
104 p5 p5.html 5
105 p6 p6.html 6
products_accounts
account_number | product_id
0000001 100
0000001 104
0000001 105
0000002 101
0000002 103
0000002 104
0000002 105
0000003 100
0000003 102
I tried a LEFT OUTER JOIN, but was not able to determine if the $accountnumber matched an account_number in the products_accounts table for a specific product_id. 我尝试了LEFT OUTER JOIN,但无法确定$ accountnumber是否与特定product_id的products_accounts表中的account_number匹配。 The only way that I was able to accomplish this was to add a WHERE statement like this: 我能够完成此操作的唯一方法是添加这样的WHERE语句:
WHERE products_acccounts.account_number = '$accountnumber'
It gave the proper class to the product, but only showed the product that they had instead of all. 它为产品提供了适当的分类,但仅显示了他们拥有的产品,而不是全部。
Here's my code: 这是我的代码:
$sql ="
SELECT
products.id,
products.name,
products.url,
products_accounts.account_number
FROM
products
LEFT OUTER JOIN
products_accounts
ON
products.id = products_accounts.product_id
";
$sql .="
GROUP BY
products.id
ORDER BY
products.weight
";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<span class="'; if($row['account_number'] == '$accountnumber')
{ echo'product_yes">'; } else { echo 'product_no">'; }
echo '<a href="' . $row['url'] . '">' . $row['name'] . '</a><br /></span>';
}
If a customer has all product except P2 and P5, it SHOULD display like this: 如果客户拥有除P2和P5以外的所有产品,则应这样显示:
✓P1 ✓P1
P2 ✓P3 ✓P4 P5 ✓P6
It's better to filter out rows using SQL than PHP, like below: 与SQL相比,使用SQL过滤掉行更好,如下所示:
$sql ="
SELECT
p.id,
p.name,
p.url,
pa.account_number
FROM
products p
LEFT OUTER JOIN
products_accounts pa
ON
p.id = pa.product_id
AND
pa.account_number = ".mysql_real_escape_string($accountnumber)."
ORDER BY
p.weight
";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<span class="'; if(!is_null($row['account_number']))
{ echo'product_yes">'; } else { echo 'product_no">'; }
echo '<a href="' . $row['url'] . '">' . $row['name'] . '</a><br /></span>';
}
SELECT
products.id,
products.name,
products.url,
products_accounts.account_number
FROM
products
LEFT OUTER JOIN
(SELECT * FROM products_accounts WHERE account_number = $account_number) as products
ON
products.id = products_accounts.product_id
WHERE
";
$sql .="
GROUP BY
products.id
ORDER BY
products.weight
";
i think this is your answer, you need to filter your join table before the join. 我认为这是您的答案,您需要在加入之前过滤您的加入表。 please check the syntax as i am not that familiar with php. 请检查语法,因为我对php不太熟悉。
You're trying to use GROUP BY
in a context that doesn't make sense if you want to retrieve all of the records. 如果您要检索所有记录,则尝试在没有意义的上下文中使用GROUP BY
。 The GROUP BY
clause should only be used if you want to aggregate data (ie get the sum, average, etc. of a bunch of records). GROUP BY
子句仅应在要汇总数据(即获取一堆记录的总和,平均值等)时使用。
$getproducts = mysql_query("
SELECT id, name, url
FROM products
ORDER BY weight ASC");
while ($rowproducts = mysql_fetch_assoc($getproducts)) {
$product_id = $rowproduct['id'];
$product_name = $rowproduct['name'];
$product_url = $rowproduct['url'];
$getuserhasproduct = mysql_query("
SELECT DISTINCT product_id
FROM products_accounts
WHERE account_number = $accountnumber
AND product_id = $product_id");
$user_has_product = mysql_num_rows($getuserhasproduct);
if($user_has_product){
$class = "checked";
}
echo "<span class='$class'><a href='$product_url'>$product_name</a></span>";
unset($class);
} // end loop
This might help with performance 这可能有助于提高性能
$getproducts = mysql_query("SELECT id, name, url,
(SELECT DISTINCT product_id
FROM products_accounts
WHERE account_number = '$accountnumber'
AND product_id = products.id) AS product_count
FROM products
ORDER BY weight ASC");
while ($rowproducts = mysql_fetch_assoc($getproducts)) {
$product_id = $rowproduct['id'];
$product_name = $rowproduct['name'];
$product_url = $rowproduct['url'];
$product_count = $rowproduct['product_count'];
if($product_count > 0){
$class = "checked";
}
echo "<span class='$class'><a href='$product_url'>$product_name</a></span>";
unset($class);
} // end loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.