我有三张桌子:
如您所见,最后两个表的结构是相同的。 " 付费"并且" 已转移"字段包含值0或1。
我需要的是创建一个应该返回所有客户端的查询(无论是否有合同),以及每个客户端: id,name, count_contracts_all , count_contracts_active
其中:
有什么想法吗?你能帮帮我吗?谢谢!
答案 0 :(得分:1)
我现在能想到的一个快速解决方案是:
SELECT COUNT(`temp_table`.*) FROM (
SELECT * FROM contracts_jewels WHERE customer_id=3 UNION ALL
SELECT * FROM contracts_objects WHERE customer_id=3) AS `temp_table`
和
SELECT COUNT(`temp_table`.*) FROM (
SELECT * FROM contracts_jewels WHERE customer_id=3 AND final_date>=Now() AND paid=0 AND transferred=0 UNION ALL
SELECT * FROM contracts_objects WHERE customer_id=3 AND final_date>=Now() AND paid=0 AND transferred=0) AS `temp_table`
答案 1 :(得分:0)
您可以单独计算合约,然后将它们加入客户:
SELECT
c.id,
COALESCE(oc.active_count,0) + COALESCE(jc.active_count,0) as count_contracts_active,
COALESCE(oc.total_count,0) + COALESCE(jc.total_count,0) as count_contracts_all
FROM customers c
LEFT JOIN (
SELECT
customer_id
COUNT(*) as total_count,
COUNT(IF(final_date>=Now() AND paid=0 AND transferred=0,1,NULL)) as active_count
FROM contracts_jewels
GROUP BY customer_id
) as oc ON oc.customer_id = c.id
LEFT JOIN (
SELECT
customer_id
COUNT(*) as total_count,
COUNT(IF(final_date>=Now() AND paid=0 AND transferred=0,1,NULL)) as active_count
FROM contracts_objects
GROUP BY customer_id
) as jc ON jc.customer_id = c.id
答案 2 :(得分:0)
您可以将每个表格加入两次,并在结果中添加相应的TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
ComponentName componentName = newComponentName("se.example.connectionservicestandalonetest", "se.example.connectionservicestandalonetest.MyConnectionService");
PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(componentName, "Admin");
PhoneAccount phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Admin").build();
telecomManager.registerPhoneAccount(phoneAccount);
:
COUNT
注意:我没有运行它,但希望它能让你朝着正确的方向前进。
答案 3 :(得分:0)
简单的解决方案:
SELECT SUM(c) FROM (
SELECT COUNT(1) as c FROM `tbl1` where ...
UNION
SELECT COUNT(1) as c FROM tbl2 where ...
UNION
SELECT COUNT(1) as c FROM tbl3 where ...
) al