我一直在四处寻找一个解决方案,这一切我发现似乎是什么,我试图做的过于复杂。
我试图让我所有的客户,在客户群#2属于的列表。据我所知,这部分工作正常。
接下来,我想看看他们的订单和产出他们公司的名字。在这里,我认为这是在我的代码引起挂起错误。
所以,最终的目标是,在第2组属于客户的公司名称的列表。
我试图通过SQL查询来做到这一点。该网页只是大干快上装载挂断了电话。有一次我得到了它的显示的一些数据,但它是不完整的。
下面是我。能否请您看看,看看有什么可能是错误的?我希望有一个刚出来的地方;或类似的东西。
*我没有粘贴连接细节出于安全原因,但代码并连接到数据库没有问题。
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Run a Query to get all wholesale customer IDs
$sql1 = "SELECT * FROM `customer_entity` WHERE `group_id` = 2";
$customerIDs = $conn->query($sql1);
//so now that we have the wholesale customer IDs we are going to search
their orders for their company names.
while( $row1 = $customerIDs->fetch_assoc() )
{
//Run a query to get a list of orders that the customer has placed
$sql2 = "SELECT * FROM `sales_flat_order_address` WHERE
`customer_id` = " . $row1["entity_id"] ."";
$customerOrders = $conn->query($sql2);
// Echo the orders company name
while( $row2 = $customerOrders->fetch_assoc() )
{
//Check to see if the name is NULL
if($row2['company'] !== NULL)
{
//display the company name.
echo $row2['company'] . "</br>";
}
}
}
//close the connection
mysqli_close($conn);
?>
最终目标是,在第2组属于客户的公司名称的列表
你可以用一个简单的JOIN
ed查询实现这一目标。从看你的代码,这应该是:
SELECT DISTINCT s.company
FROM
customer_entity c
INNER JOIN sales_flat_order_address s ON s.customer_id = c.entity_id
WHERE c.group_id = 2
该查询将检索所有用户组2,然后选择他们所有的订单,最后输出所有(不同),对应的公司。