mysql搜索2个表重叠

时间:2013-06-04 22:17:15

标签: php mysql join

我有2个表,见下文 - 个人资料是我的主表/主表

  profiles            invoices
 ____________       ___________________
|id  Name    |     |profileid   paid   |
|============|     |===================|
|1  Abraham  |     |  2         unpaid |
|2  Martin   |     |  3         unpaid |
|3  John     |     |  3         paid   |
|____________|     |___________________|
可以看出,亚伯拉罕有0张发票,马丁有1张未付发票,约翰有2张发票; 1付,1未付。

我想搜索:

  1. 带付款发票的所有个人资料(约翰)
  2. 所有未付发票的配置文件(john& martin)
  3. 包含付费和未付款发票(约翰)的所有个人资料
  4. 我可以做1和2罚款,但我对第3步有问题。

    这是我对1的查询;

    $query = "SELECT DISTINCT profiles.name 
    FROM profiles LEFT JOIN invoices ON (profiles.id=invoices.profileid) 
    AND (invoices.paid='paid' OR invoices.paid='unpaid')
    WHERE
    IFNULL(invoices.paid, '') LIKE 'paid';
    

    这是我对2的查询;

    $query = "SELECT DISTINCT profiles.name 
    FROM profiles LEFT JOIN invoices ON (profiles.id=invoices.profileid) 
    AND (invoices.paid='paid' OR invoices.paid='unpaid')
    WHERE
    IFNULL(invoices.paid, '') LIKE 'unpaid';
    

    这是我对3的查询;

    $query = "SELECT DISTINCT profiles.name 
    FROM profiles LEFT JOIN invoices ON (profiles.id=invoices.profileid) 
    AND (invoices.paid='paid' OR invoices.paid='unpaid')
    WHERE
    IFNULL(invoices.paid, '') LIKE 'paid' 
    AND IFNULL(invoices.paid, '') LIKE 'unpaid'
    ;
    
    如上所述,1& 2工作正常,但3给我0结果。 任何帮助深表感谢。感谢

3 个答案:

答案 0 :(得分:2)

您需要选择invoices表2次,才能获得付费和未付款的位置。尝试像 -

这样的东西
SELECT DISTINCT profiles.name 
FROM profiles 
LEFT JOIN invoices i1 ON (profiles.id=i1.profileid) 
AND (i1.paid='paid')
LEFT JOIN invoices i2 ON (profiles.id=i2.profileid) 
AND (i2.paid='unpaid')
WHERE
IFNULL(i1.paid, '') LIKE 'paid' 
AND IFNULL(i2.paid, '') LIKE 'unpaid';

请参阅此sqlfiddle示例 - http://sqlfiddle.com/#!2/ee4e2/4

答案 1 :(得分:0)

一种可能性是子查询来计算与个人资料相关联的不同发票类型

SELECT `profiles`.`name`
FROM `profiles`
WHERE (
  SELECT count( DISTINCT `subinvoices`.`paid` )
  FROM `invoices` AS `subinvoices`
  WHERE `profiles`.`id` = `subinvoices`.`profileid`
    AND (`subinvoices`.`paid` = 'paid' OR `subinvoices`.`paid` = 'unpaid')
) = 2

答案 2 :(得分:0)

最简单的方法是使用聚合:

select p.*, coalesce(InvoiceInfo, 'NONE')
from profiles p left outer join
     (select profileid,
             (case when sum(paid = 'PAID') > 0 and sum(paid = 'UNPAID') > 0 then 'BOTH'
                   when sum(paid = 'PAID') > 0 then 'PAID-ONLY'
                   when sum(paid = 'UNPAID') > 0 then 'UNPAID-ONLY'
              end) as InvoiceInfo
      from invoices i
     )
     on p.id = i.profileid

这使您可以一次获取所有三种类型的信息。例如,您可以通过执行以下操作来统计每个组中的人员:

select coalesce(InvoiceInfo, 'NONE'), count(*)
from profiles p left outer join
     (select profileid,
             (case when sum(paid = 'PAID') > 0 and sum(paid = 'UNPAID') > 0 then 'BOTH'
                   when sum(paid = 'PAID') > 0 then 'PAID-ONLY'
                   when sum(paid = 'UNPAID') > 0 then 'UNPAID-ONLY'
              end) as InvoiceInfo
      from invoices i
     )
     on p.id = i.profileid
group by coalesce(InvoiceInfo, 'NONE')