如何显示SQL表中的所有记录并将这些记录与另一个表匹配?

时间:2012-01-24 18:41:35

标签: php mysql sql

我已将PHP变量$ accountnumber设置为正在查看其个人资料页面的用户。在页面上,我有一个块,其中包含从数据库填充的用户信息,我有一个包含我们所有产品的列表,并且我想在客户所拥有的每个产品旁边放一个复选标记,方法是将一个类分配给它。

以下是我的表格:

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

我尝试了LEFT OUTER JOIN,但无法确定$ accountnumber是否与products_accounts表中的account_number匹配特定product_id。我能够实现此目的的唯一方法是添加如下的WHERE语句:

WHERE products_acccounts.account_number = '$accountnumber'

它为产品提供了适当的等级,但只展示了他们拥有的产品而不是所有产品。

这是我的代码:

$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>';
}

如果客户拥有除P2和P5之外的所有产品,它应该显示如下:

  

✓P1

P2

✓P3

✓P4

P5

✓P6

4 个答案:

答案 0 :(得分:2)

最好使用SQL过滤掉行而不是PHP,如下所示:

$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>';
}

答案 1 :(得分:1)

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
";

我认为这是您的答案,您需要在加入之前过滤您的联接表。请检查语法,因为我不熟悉php。

答案 2 :(得分:0)

如果要检索所有记录,您尝试在没有意义的上下文中使用GROUP BY。只有在想要聚合数据时才能使用GROUP BY子句(即得到一堆记录的总和,平均值等)。

答案 3 :(得分:0)

$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 

这可能对性能有帮助

$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