多个表左连接返回相同的值

时间:2015-10-21 21:14:04

标签: php mysql arrays

是否可以进行一次返回第二个表的值并将其他值设置为NULL的查询。我坚持这个。

这是我的查询

return $this->db->get_results(
        "
        SELECT id, name, type, check_in_days, check_out_days, all_check_out_days, minimum_stay, maximum_stay, all_accom, GROUP_CONCAT( accom_id ) as accom, GROUP_CONCAT( seasons_id ) as seasons, conditional_type
        FROM $this->booking_rules_table
        LEFT JOIN $this->booking_rules_accom_table
        ON $this->booking_rules_table.id = $this->booking_rules_accom_table.rule_id
        LEFT JOIN $this->booking_rules_seasons_table
        ON $this->booking_rules_table.id = $this->booking_rules_seasons_table.rule_id
        GROUP BY id
        "
    , ARRAY_A );

这将返回一个这样的数组

[2] => Array
            (
                [id] => 54
                [name] => 
                [type] => minimum_stay
                [check_in_days] => 0,1,2,3,4,5,6
                [check_out_days] => 0,1,2,3,4,5,6
                [all_check_out_days] => 1
                [minimum_stay] => 0
                [maximum_stay] => 9999
                [all_accom] => 0
                [accom] => 7,7,7
                [seasons] => 1,3,4
                [conditional_type] => compulsory
            )

你看到[住宿]返回7次,因为[季节]有3个值。

我可以用我的查询修复此问题,还是有其他解决方案。我不想爆炸它并再次构建阵列。

0 个答案:

没有答案