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 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题