简体   繁体   中英

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. 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. The only way that I was able to accomplish this was to add a WHERE statement like this:

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:

✓P1

 P2 ✓P3 ✓P4 P5 ✓P6 

It's better to filter out rows using SQL than PHP, like below:

$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.

You're trying to use GROUP BY in a context that doesn't make sense if you want to retrieve all of the records. 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).

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM