我需要将三个表连接在一起。
1- payments
表:
id
member_id
year_id
notes
paid
paid_at
created_at
updated_at
2- yearly_fees
表:
id
year
amount
created_at
updated_at
3- members
表:
id
first_name
last_name
我要做的是显示所有在xxxx年内已付款和未付款的成员的列表。
预期的示例输出:
id first_name father_name notes paid year amount
1 test name test last_name test note 1 2018 3000
2 test name test last_name test note NULL NULL NULL
3 test name test last_name test note 1 2018 3000
4 test name test last_name NULL NULL NULL NULL
5 test name test last_name NULL NULL NULL NULL
这是我编写的查询:
SELECT `members`.`id`, `members`.`first_name`, `members`.`last_name`,
`payments`.`notes`, `payments`.`paid`, `yearly_fees`.`year`,
`yearly_fees`.`amount` FROM `members`
LEFT JOIN payments ON payments.member_id = members.id
LEFT JOIN yearly_fees ON yearly_fees.id = payments.year_id
WHERE payments.year_id = 4
结果:
id first_name father_name notes paid year amount
1 test name test last_name test note 1 2018 3000
2 test name test last_name test note 1 2018 3000
3 test name test last_name test note 1 2018 3000
WHERE
语句仅输出与payments
表匹配的行,但我希望它也输出每个成员,即使其余行的结果为NULL。如果我删除WHERE
语句,它的工作方式完全符合我的要求,但是却使我花了很多年而不是我特别想要的。
这是示例输出:
id first_name father_name notes paid year amount
1 test name test last_name test note 1 2016 3000
2 test name test last_name test note 1 2015 3000
3 test name test last_name test note 1 2018 3000
4 test name test last_name test note 1 2018 3000
5 test name test last_name test note 1 2018 3000
6 test name test last_name NULL NULL NULL NULL
7 test name test last_name NULL NULL NULL NULL
提前为英语不好而道歉。
答案 0 :(得分:0)
您需要将条件移至bundle agent loadbalancers{
files:
ubuntu::
"/etc/nginx/nginx.conf"
create => "true",
edit_template => "$(this.promise_dirname)/../templates/nginx.conf.mustache",
template_method => "mustache",
template_data => parsejson('
{
"worker_processes": "auto",
"worker_rlimit_nofile": 32768,
"worker_connections": 16384,
}
');
}
子句:
ON
如果首先更适合SELECT `m`.`id`, `m`.`first_name`, `m`.`last_name`,
`p`.`notes`, `p`.`paid`, `yf`.`year`, `yf`.`amount`
FROM `members` m JOIN
yearly_fees yf
ON yf.year_id = 4 LEFT JOIN
payments p
ON p.member_id = m.id AND p.year_id = yf.id;
JOIN
,因为该匹配项应始终存在。然后使用yearly_fees
查看LEFT JOIN
中是否有匹配的行。