如何为wp_rest_api联接两个MySQL表

在我的WordPress数据库中,我有三个表wp_companies和wp_product_types,这些表存储一些公司信息,而wp_product_types存储一堆产品类型,公司可以自行分配它们,然后我有wp_company_products,用于使用ID将产品类型分配给公司。

以下是数据库外观的示例:

wp_companies

id | company_name | member_type | logo
-----------------------------------------------------------------
1  | Google       | full        | https://via.placeholder.com/150
-----------------------------------------------------------------
2  | Crunchyroll  | full        | https://via.placeholder.com/150

wp_products

id | product_name    |
----------------------
1  | Car Insurance   |
----------------------
2  | House Insurance |
----------------------
3  | Life Insurance  |

wp_company_products

id | company_id | product_id 
----------------------------
1  | 1          | 2
----------------------------
2  | 2          | 1
----------------------------
3  | 1          | 3

这是我当前的MySQL查询,仅显示wp_companies中的数据

    add_action('rest_api_init', function() {
        register_rest_route('custom-routes/v1', 'members', array(
            'methods' => 'GET',
            'callback' => 'get_members'
        ) );
    });

    function get_members($data) {
        global $wpdb;

        $query = $wpdb->get_results( "SELECT company_name, member_type, logo, site_url FROM {$wpdb->prefix}companies ORDER BY RAND()" );

        foreach ( $query as $member ) {
            $member_data[] = array(
                "company_name" => $member->company_name,
                "member_type" => $member->member_type,
                "logo" => $member->logo,
            );
        }

        return $member_data;
    }

这样会在我的api端点上显示我的数据:

[
    {
        "company_name":"Google",
        "member_type":"full",
        "logo":"https://via.placeholder.com/150",
    },
    {
        "company_name":"Crunchyroll",
        "member_type":"full",
        "logo":"https://via.placeholder.com/150",
    }
]

但是我想要的是结合wp_companies和wp_company_products表,以便我的数据在api端点上显示如下:

[
    {
        "company_name":"Google",
        "member_type":"full",
        "logo":"https://via.placeholder.com/150",
        "products": [
            "House Insurance",
            "Life Insurance"
         ]
    },
    {
        "company_name":"Crunchyroll",
        "member_type":"full",
        "logo":"https://via.placeholder.com/150",
        "products": [
            "Car Insurance",
         ]
    }
]

如何构造MySQL查询以实现此目的?