Android 条件选择哪个表-SQL或SQLITE
我创建了以下表格: 客户表:Android 条件选择哪个表-SQL或SQLITE,android,sql,sqlite,Android,Sql,Sqlite,我创建了以下表格: 客户表: flag | cust_id | customer -------------------------------- 1 | 60 | customer1 0 | 61 | customer2 0 | 62 | customer3 账户表: acc_id | cust_id | account_name ------------------------
flag | cust_id | customer
--------------------------------
1 | 60 | customer1
0 | 61 | customer2
0 | 62 | customer3
账户表:
acc_id | cust_id | account_name
------------------------------------
1 | 62 | account1
2 | 63 | account2
3 | 62 | account3
4 | 62 | account4
联系人表:
con_id | cust_id | contact_name
------------------------------------
1 | 60 | contact1
2 | 60 | contact2
3 | 61 | contact3
4 | 62 | contact4
如果客户表中的flag==1
,则我需要Accounts表中的所有行,否则flag==0
,则我需要Contacts表中的所有行
如何在一个声明中实现这一点
有人能帮我吗?我想你需要两个连接的并集。
比如:
-- SELECT the items from Customer and Accounts WHERE flag = 1
SELECT Customer.customer, Accounts.account_name AS Name FROM Customer
INNER JOIN Accounts ON Customer.cust_id = Accounts.cust_id
WHERE Customer.flag = 1
-- MERGE the 2 queries WITHOUT duplicates (use UNION ALL if you want the duplicates)
UNION
-- SELECT the items from Customer and Contacts WHERE flag = 0
SELECT Customer.customer, Contacts.contacts_name AS Name FROM Customer
INNER JOIN Contacts ON Customer.cust_id = Contacts.cust_id
WHERE Customer.flag = 0
您可能需要将Customer.cust\u id添加到两个查询列列表中。在我看来,您似乎需要两个jointhanks的联合来提供帮助,Golem。在修改了查询中的一些条件后,我得到了解决方案。再次感谢。