尝试在 Magento 1.7 数据库中查询客户 "company" 值

Trying to query for customers "company" value in Magento 1.7 DB

我一直在四处寻找解决方案,但我发现的一切对于我正在尝试做的事情来说似乎都过于复杂。

我正在尝试获取属于客户组 #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);

?>

the end goal is a list of all company names for customers that belong in group 2

您可以通过一个简单的 JOINed 查询来实现。通过查看您的代码,这应该是:

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 中的所有用户,然后 select 他们的所有订单,最后输出所有(不同的)对应公司。