我有这些桌子:
transactions
addonlist
addons
我想echo上的所有项目,并查看表addons中的addon_id是否与表addons中的laddon_id匹配,以便为跟踪的项目添加注释。
我有这个代码,我可以对addonlist中找到的项目做一个注释,但它与transac_id中的相同项目。

    $getaddons = mysql_query("SELECT * FROM addons LIMIT 10");
    while($rows = mysql_fetch_assoc($getaddons)){
        $addonid = $rows['addon_id'];
        $addondesc = $rows['description'];
        $addonprice = $rows['price'];
        $addonstat = $rows['status'];
        $checkaddon = mysql_query("SELECT * FROM transactions t, addonlist al WHERE t.transac_id='44005' and t.transac_id=al.transac_id");
        while($rows = mysql_fetch_assoc($checkaddon)){
            $caddonid = $rows['laddon_id'];
            if(mysql_num_rows($checkaddon) and $addonid == $caddonid){
                echo "$addondesc already in your list"; // NOTE: item is already in your list
            }
        }
        echo "<strong>$addondesc </strong><button>Add to list</button>";
    }

这将显示(我的期望):
Coke 1 Litre - already in your list
Tuna Sandwich - already in your list
Hotdog Sanwich - add button
Chicken Sandwich - add button
Ham & Egg Sandwich - add button
Ham & Cheese Sandwich - add button
Grilled Cheese Burger - add button
Clubhouse Sandwich - add button
Goto - add button
Arrozcaldo - add button

但它显示的是:
Coke 1 Litre - already in your list
Coke 1 Litre - add button `// This wouldn't be appearing`
Tuna Sandwich - already in your list
Tuna Sandwich - add button `// This wouldn't be appearing`
Hotdog Sanwich - add button
Chicken Sandwich - add button
Ham & Egg Sandwich - add button
Ham & Cheese Sandwich - add button
Grilled Cheese Burger - add button
Clubhouse Sandwich - add button
Goto - add button
Arrozcaldo - add button

编辑:
请告诉我我的addonlist是坏的还是仅仅是我的echoes

最佳答案

使用LEFT JOIN在一个查询中获取所有内容。对于不在addonlist中的加载项,您将得到NULL中列的addonlist,您可以在循环中测试它。

$getaddons = mysql_query("SELECT a.addon_id, a.description, a.price, a.status, l.laddon_id
                          FROM addons AS a
                          LEFT JOIN addonlist AS l ON a.addon_id = l.laddon_id AND l.transac_id = '44005'
                          LIMIT 10");
while ($rows = mysql_fetch_assoc($getaddons)) {
    $addonid = $rows['addon_id'];
    $addondesc = $rows['description'];
    $addonprice = $rows['price'];
    $addonstat = $rows['status'];
    if ($rows['laddon_id']) {
        echo "$addondesc already in your list";
    } else {
        echo "<strong>$addondesc </strong><button>Add to list</button>";
    }
}

似乎不需要加入transactions,因为您没有使用该表中的任何内容。

07-26 09:36