[英]How to join three tables in SQLite?
How to join three tables in SQLite?如何在 SQLite 中连接三个表? I have three tables, one is
Info
, second is workForce
and third is workDetails
.我有三个表,一个是
Info
,第二个是workForce
,第三个是workDetails
。
Table Info: id(PK),name,status,date,weather
表格信息:
id(PK),name,status,date,weather
Table WorkForce: id1(PK), subContractors,noOfPeople,noOfHours
表劳动力:
id1(PK), subContractors,noOfPeople,noOfHours
Table WorkDetails: id2(PK),project,workDescription,TableInfo_id(FK) //contains multiple row
Table WorkDetails:
id2(PK),project,workDescription,TableInfo_id(FK) //contains multiple row
Table Info表格信息
ID NAME Weather Date Status
---------- ---------- ---------- ---------- ----------
1 Paul Sunny 15/10 MC
2 Allen Rainy 15/10 Working
Table WorkForce表劳动力
ID1 SubContractors NoOfPeople NoOfHours
---------- -------------- ---------- ----------
1 AAA 2 2
2 BBB 3 1
Table WorkDetails表工作详细信息
ID2 Project WorkDescription TableInfo_id
---------- ---------- -------------- ----------
1 A B 1
2 1
3 1
4 1
5 C D 2
6 2
7 2
8 2
Assume the name is Paul, so all the row with ID 1 and TableInfo_id 1 will be retrieved.假设名字是 Paul,那么所有 ID 为 1 且 TableInfo_id 为 1 的行都将被检索。
Here is what I tried so far这是我到目前为止尝试过的
public Cursor readEntry(String name) {
String selectQuery = ("SELECT Weather,Date,Status,SubContractors,NumberOfPeople,NumberOfHours,TimeIn,TimeOut FROM "+TABLE_INFO+TABLE_WORKFORCE+TABLE_WORKDETAILS+ "WHERE Name= ? AND"+ID=ID1+ "AND"+ID=TableInfo_id);
Cursor c = database.query(TABLE_INFO,TABLE_WORKFORCE,TABLE_WORKDETAILS,new String[]{id,name,weather,date,status,iD1,subcontractors,numberOfPerson,numberOfHours,id2project,workDescription,TableInfo_id},MyDatabaseHelper.Name+"=?",
new String[] { String.valueOf(name)}, null, null, null, null,null,null,null,null,null,null,null,null);
if (c != null) {
c.moveToFirst();
}
return c;
}
My code seems like not working..how can I achieve this?我的代码似乎不起作用..我怎样才能做到这一点? Thanks!
谢谢!
Fist thing you need to do is to add foreign key of Table Info into Table WorkForce and foreign key of Table WorkForce into Table WorkDetails你需要做的事情拳是添加表信息的外键表的员工队伍和员工表的外键表WorkDetails
Then write modify your query like this然后像这样写修改你的查询
Select * from Table Info tf
LEFT JOIN Table WorkForce twf ON twf.tf_id = tf.id
LEFT JOIN Table WorkDetails twd ON twd.tw_id = twf.id
Modify your query based on requirement after joining the three tables.加入三个表后,根据要求修改您的查询。
Check out the tutorials for adding a foreign key.查看添加外键的教程。
Think that should work have not tried it tho认为应该工作还没有尝试过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.