【问题标题】:Trying to query for customers "company" value in Magento 1.7 DB尝试在 Magento 1.7 DB 中查询客户“公司”价值
【发布时间】:2019-02-03 01:26:23
【问题描述】:

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

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

?>

【问题讨论】:

    标签: php mysql sql magento


    【解决方案1】:

    最终目标是列出属于第 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 中的所有用户,然后选择他们的所有订单,最后输出所有(不同)对应的公司。

    【讨论】:

    • 非常感谢。我需要学习和了解更多关于 JOIN-ed 查询的信息。似乎我把事情复杂化了。谢谢,谢谢,谢谢,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2023-03-10
    • 2023-03-31
    相关资源
    最近更新 更多