我有4张桌子:
我希望得到转移或付款的客户。付款表链接到客户表,转移表链接到帐户表,帐户表链接到客户表。
如何编写返回付款或转帐客户的查询?
SELECT customer.registration_os, Count(DISTINCT customer.customer_id) AS registrations
FROM customer
Inner Join account On customer.customer_id = account.customer_id
INNER JOIN payment ON customer.customer_id = payment.customer_id
LEFT JOIN transfer ON customer.customer_id= transfer.from_account
WHERE customer.ts_created BETWEEN ? AND ?
AND customer.registration_source = ?
GROUP BY customer.registration_os
谢谢
答案 0 :(得分:0)
SELECT * FROM Customers as t1
LEFT JOIN Accounts as t2 ON t2.customer_id = t1.id // (Account table is linked to Customers)
LEFT JOIN Payments as t3 ON t3.customer_id = t3.id // (Payments table is linked to Customers)
LEFT JOIN Transfers as t4 ON t4.account_id = t2.id // (Transfers table is linked to Accounts)
WHERE (condition)
对于基于表名的条件使用参数,我已经给出了t1,t2,t3,t4你可以使用条件中的任何表。
答案 1 :(得分:0)
我希望得到转帐或付款的客户
在SQL中,UNION
类似于逻辑OR
:
SELECT customer_id
FROM customer NATURAL JOIN account
UNION
SELECT customer_id
FROM customer NATURAL JOIN ( SELECT from_account AS customer_id
FROM transfer ) t