doouzlrvb01417498 2019-03-17 15:41
浏览 185
已采纳

PHP MYSQL联合查询

Im new to mysql queries and im having trouble obtaining the data structure I want. Im hoping one of you guys can help.

I have the following query:

SELECT *
FROM wp_postmeta
WHERE post_id IN (SELECT id from wp_posts WHERE post_type = 'product')

Which simply grabs all the data from Table 2 that has the same id as the product in Table 1. The problem is that the results return as:

{meta_id: "37230", post_id: "2549", meta_key: "total_sales", meta_value: "0"}
{meta_id: "37231", post_id: "2549", meta_key: "_virtual", meta_value: "no"}
{meta_id: "37232", post_id: "2549", meta_key: "_tax_status", meta_value: "taxable"}
{meta_id: "37233", post_id: "2549", meta_key: "_visibility", meta_value: "visible"}
{meta_id: "37234", post_id: "2549", meta_key: "_featured", meta_value: "no"}
{meta_id: "37235", post_id: "2549", meta_key: "_weight", meta_value: "0.50"}
{meta_id: "37236", post_id: "2549", meta_key: "_sku", meta_value: "HCS-DGMP"}
{meta_id: "37237", post_id: "2549", meta_key: "_product_attributes", meta_value: "a:0:{}"}
{meta_id: "37238", post_id: "2549", meta_key: "_regular_price", meta_value: "18.99"}
{meta_id: "37239", post_id: "2549", meta_key: "_sale_price", meta_value: ""}

As you can see, they all have the same post ID, but are returned in seperate objects. How can I modify my query above so that all of that returns in one object? I'll also be removing the meta_id as it is useless.

  • 写回答

1条回答 默认 最新

  • doupu1727 2019-03-17 16:44
    关注

    Since MySQL 5.7.22 you can use JSON_OBJECTAGG(key, value):

    SELECT post_id, JSON_OBJECTAGG(meta_key, meta_value) AS meta_data
    FROM wp_postmeta
    WHERE post_id IN (SELECT id from wp_posts WHERE post_type = 'product')
    GROUP by post_id
    

    This will return meta_data as JSON_OBJECT with key: value pairs:

    post_id | meta_data
       2549 | {"_sku": "HCS-DGMP", "_weight": "0.50", "_virtual": "no", "_featured": "no", "_sale_price": null, "_tax_status": "taxable", "_visibility": "visible", "total_sales": "0", "_regular_price": "18.99", "_product_attributes": "a:0:{}"}
    

    Demo

    If your server doesn't support JSON_OBJECTAGG(), I would use a PHP solution:

    $postIds = array_unique(array_column($dbResult, 'post_id'));
    
    $posts = [];
    foreach ($postIds as $id) {
        $posts[$id] = (object)['id' => $id];
    }
    
    foreach ($dbResult as $row) {
        $posts[$row->post_id]->{$row->meta_key} = $row->meta_value;
    }
    
    echo json_encode(array_values($posts), JSON_PRETTY_PRINT);
    

    Result:

    [
        {
            "id": "2549",
            "total_sales": "0",
            "_virtual": "no",
            "_tax_status": "taxable",
            "_visibility": "visible",
            "_featured": "no",
            "_weight": "0.50",
            "_sku": "HCS-DGMP",
            "_product_attributes": "a:0:{}",
            "_regular_price": "18.99",
            "_sale_price": ""
        }
    ]
    

    Demo

    As long as you are not processing thousands of rows, I wouldn't worry about performance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理