PHP MySQL使用条件连接连接两个表

时间:2014-10-28 07:39:50

标签: php mysql join

所以,我有一个我正在创建的数据库。它存储有关家庭和每个家庭成员的信息。然后,它使用这些记录将发票与家庭成员或家庭成员相关联。

我的困境是我需要将所有这些发票列入家庭记录下的页面,即创建与家庭本身或个人家庭成员相关的发票清单。

表格结构

发票

id | date_entered | invoice_date | invoice_number | invoice_amount | client_type | unique_id | supplier_type | supplier_id | category_id | childcare_hours
---+--------------+--------------+----------------+----------------+-------------+-----------+---------------+-------------+-------------+----------------
1  | 1411098397   | 1411048800   | 123            | 0.01           | 0           | 137       | 0             | 139         | 5           | NULL

家庭

id |  ufi     |   last_name     | address_1   | address_2 | city_id | phone | mobile | email | f_d_worker_1 | f_d_worker_2 | status_id | trans_date | entry_date | exit_date | eligible_date | active_date | lga_loc_id | facs_loc_id | ind_status_id | referral_id | active_status | comm_org_id | notes
---+----------+-----------------+-------------+-----------+---------+-------+--------+-------+--------------+--------------+-----------+------------+------------+-----------+---------------+-------------+------------+-------------+---------------+-------------+---------------+-------------+-------
1  | 1-XEWUDZ | Forsyth - Ennis | Skinner St. | NULL      | NULL    | NULL  | NULL   | NULL  | 13           | NULL         | 1         | NULL       | 1341324000 | NULL      | 1341842400    | 1342620000  | 7          | 1           | 3             | NULL        | 1             | 1           | NULL

客户(家庭成员)

id |   upi    | last_name | first_name | birthdate  | sex | phone | mobile | email | indig_status_id | referral_id | relationship_id | preschool_id | family_id | notes
---+----------+-----------+------------+------------+-----+-------+--------+-------+-----------------+-------------+-----------------+--------------+-----------+------
 1 | 1-XFCBBP | Ennis     | Jason      | 20/09/1996 | 1   | NULL  | NULL   | NULL  | 3               | NULL        | NULL            | NULL         | 1         | NULL

我当前的SQL看起来像:

SELECT `invoices`.`id`, `invoices`.`date_entered`, `invoices`.`invoice_date`, `invoices`.`invoice_number`, `invoices`.`invoice_amount`, `invoices`.`client_type`, `invoices`.`unique_id`, `unique1`.`ufi`, `unique2`.`upi`, `unique1`.`last_name`, `invoices`.`supplier_type`, `invoices`.`supplier_id`, `suppliers`.`name`,  `invoices`.`category_id`, `cat1`.`name`, `cat2`.`name`, `invoices`.`childcare_hours` 
FROM `invoices` 
LEFT OUTER JOIN `suppliers` ON `suppliers`.`id` = `invoices`.`supplier_id` 
LEFT OUTER JOIN `categories` cat1 ON `cat1`.`id` = `invoices`.`category_id` 
LEFT OUTER JOIN `preschool_types` cat2 ON `cat2`.`id` = `invoices`.`category_id` 
LEFT OUTER JOIN `families` unique1 ON `unique1`.`id` = `invoices`.`unique_id` 
LEFT OUTER JOIN `clients` unique2 ON `unique2`.`id` = `invoices`.`unique_id` 
WHERE (`invoices`.`unique_id` = ? AND `unique1`.`ufi` = ?) LIMIT 0, 10

但我需要一个检查client_type列的查询,如果它等于1,则需要查看clients表,但是需要查找同一系列的成员,由families表中的id行

好的,经过多次,很多(很多)搞砸了一点研究。似乎@cupid是正确的(尽管他的回答非常简短)。

我会更好地解释解决方案(希望以后能帮助别人)。

MySQL中的UNION选项(很可能是其他SQL)允许您将两个(或更多)SELECT查询的结果集合并到一个结果集中。如果您有相似的数据,这可能非常有用,您可以在单独的表中轻松选择并作为一个请求进行处理。通过允许您使用SQL的LIMIT选项,对于分页也很有用(在我的情况下)。

要考虑的一点是,UNION语法使用第一个SELECT语句中的列作为所有后续查询的列名,还需要确保在所有查询中选择了相同数量的列为了这个工作。

(
    SELECT 
        `invoices`.`id`, 
        `invoices`.`date_entered`, 
        `invoices`.`invoice_date`, 
        `invoices`.`invoice_number`, 
        `invoices`.`invoice_amount`, 
        `invoices`.`client_type`, 
        `invoices`.`unique_id`, 
        `clients`.`upi`, 
        `clients`.`last_name`, 
        `clients`.`family_id`,
        `invoices`.`supplier_type`, 
        `invoices`.`supplier_id`, 
        `suppliers`.`name`, 
        `invoices`.`category_id`, 
        `cat1`.`name`, 
        `cat2`.`name`, 
        `invoices`.`childcare_hours` 
    FROM
    (
        `invoices` 
            LEFT OUTER JOIN `suppliers` ON `suppliers`.`id` = `invoices`.`supplier_id`
            LEFT OUTER JOIN `categories` cat1 ON `cat1`.`id` = `invoices`.`category_id`
            LEFT OUTER JOIN `preschool_types` cat2 ON `cat2`.`id` = `invoices`.`category_id`
            LEFT OUTER JOIN `clients` ON `clients`.`id` = `invoices`.`unique_id`)
        WHERE 
            `clients`.`family_id` = 47 AND `invoices`.`client_type` = 1
    )
UNION
( 
    SELECT 
        `invoices`.`id`,
        `invoices`.`date_entered`, 
        `invoices`.`invoice_date`, 
        `invoices`.`invoice_number`, 
        `invoices`.`invoice_amount`, 
        `invoices`.`client_type`, 
        `invoices`.`unique_id`, 
        `families`.`ufi`, 
        `families`.`last_name`,
        `families`.`id`,
        `invoices`.`supplier_type`, 
        `invoices`.`supplier_id`, 
        `suppliers`.`name`, 
        `invoices`.`category_id`, 
        `cat1`.`name`, 
        `cat2`.`name`, 
        `invoices`.`childcare_hours` 
    FROM `invoices`
        LEFT OUTER JOIN `suppliers` ON `suppliers`.`id` = `invoices`.`supplier_id` 
        LEFT OUTER JOIN `categories` cat1 ON `cat1`.`id` = `invoices`.`category_id`
        LEFT OUTER JOIN `preschool_types` cat2 ON `cat2`.`id` = `invoices`.`category_id`
        LEFT OUTER JOIN `families` ON `families`.`id` = `invoices`.`unique_id`
    WHERE 
        `invoices`.`unique_id` = 47 AND `invoices`.`client_type` = 0
)

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用UNION?