我有 2 个表,见下文 - 配置文件是我的主表/主表
profiles invoices
____________ ___________________
|id Name | |profileid paid |
|============| |===================|
|1 Abraham | | 2 unpaid |
|2 Martin | | 3 unpaid |
|3 John | | 3 paid |
|____________| |___________________|
可以看出,abraham 有 0 张发票,martin 有 1 张未付发票,john 有 2 张发票;1个已付,1个未付。
我想搜索:
- 所有带有已付发票的个人资料 (john)
- 所有带有未付发票的个人资料 (john & martin)
- 所有带有已付和未付发票的个人资料 (john)
我可以很好地完成第 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 结果。任何帮助深表感谢。谢谢