-1

如何比较同一数据库中的 2 个表数据。例如:

Table 1:
--------
Order number, user_id etc

Table 2:
--------
Order number

=> 一个用户可以下多于一个订单。所以我需要用表 2 的订单号检查用户的所有订单。如果找到,那么它将打印 true 否则为 false。我正在尝试以下代码:

$user = JFactory::getUser();
$user_id= $user->get('id');

$db = JFactory::getDbo();
$db->setQuery("SELECT virtuemart_user_id, order_number, order_total, created_on FROM `#__virtuemart_orders` WHERE `virtuemart_user_id`='$user_id' AND `order_status`='C'");
$row = $db->loadRowList();

foreach($row as $d){

// I NEED TO COMPARE THE LOGIC HERE WITH THE SECOND TABLE. If it found match 
// with table 2 then another column will be created & print out that 
// "We found your order"

echo "<tr>";
echo "<td><a href='index.php?option=com_virtuemart&view=orders&layout=details&order_number=".$d['1'] ."'target='_blank'>".$d['1']."</a></td>";
echo "<td><a href='index.php?option=com_virtuemart&view=orders&layout=details&order_number=".$d['1'] ."'target='_blank'>".$d['2']."</td>";
echo "<td><a href='index.php?option=com_virtuemart&view=orders&layout=details&order_number=".$d['1'] ."'target='_blank'>".$d['3']."</td>";
echo "</tr>";

}
4

1 回答 1

0

您可以在查询中使用 sql 执行此操作:

就像是

SELECT virtuemart_user_id, user_name, COUNT(virtuemart_order_id) AS num_orders
FROM virtuemart_users
LEFT JOIN virtuemart_orders USING (virtuemart_user_id)
WHERE `virtuemart_user_id`='$user_id' AND `order_status`='C'

将给出一个表格,其中包含每个用户的 id、名称和已完成订单的数量(实际上是我们设法匹配的行数)。

您可以使用相同的逻辑从 meadowmart_orders 表转到另一个表,如果您只想要两个表都具有共享 id 的行,则可以使用 aJOIN而不是 a 。LEFT JOIN

于 2013-07-28T15:12:03.673 回答