我创建了以下表格:
客户表:
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,那么我需要使用以下条件来获取数据:我需要客户表中的所有行,否则标志== 0,那么我需要联系人表中的所有行.
如何一口气做到这一点?
有人可以帮我吗?
解决方法:
我猜您需要2个JOIN的UNION.
就像是:
-- 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_id添加到两个查询列列表中.
标签:sqlite,sql,android 来源: https://codeday.me/bug/20191121/2053712.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。