使用多个表获取错误的输出加入sqlite

时间:2015-03-03 06:07:22

标签: android sqlite join multiple-tables

朋友您好我有5张表,如下所示

property_master --> "p_id" , "p_name" , "p_address" , "p_city" , "p_state" ,"r_id"

property_unit -->"unit_id"   , "p_id"  , "unit_name"   ,"r_id"

unit_info -->    "unit_info_id"   ,"unit_id" INTEGER,"p_id"   ,"p_bathroom"  ,"p_bedroom"  ,"p_size"  ,"p_rent"  ,"p_isrent"  ,"u_note" ,"r_id" 

tanant_master -->  "t_id"   , "t_name"  ,"t_cell_no"  ,"t_phone_no"  ,"t_mail"  ,"t_deposit"   ,"r_id"

property_assign-->  "t_assign_id"   , "unit_info_id"  ,  "t_id"  , "t_start_date"  , "t_end_date"  , "  t_rent_due_day"  , "t_lease_alert"  , "t_status"  ,"r_id"

我的查询如下

SELECT property_master.p_id AS "PID",
property_master.p_name AS "PropertyName",
property_master.p_address AS "ProepertyAddress",
property_master.p_city AS "ProepertyCity",
property_master.p_state AS "ProepertyState",
property_master.p_country AS "ProepertyCountry",

property_unit.p_id AS "PUID",
property_unit.unit_name AS "UnitName",
property_unit.unit_id AS "UnitID",

unit_info.p_id AS "UNPID",
unit_info.p_bathroom AS "UNBath",
unit_info.p_bedroom AS "UNBEd",
unit_info.p_size AS "UNSize",
unit_info.p_rent AS "UNRent",
unit_info.u_note AS "UNNOte",

tanant_master.p_id AS "TPID",
tanant_master.t_name AS "TPID",

property_assign.unit_info_id AS "UNITINFOID",
property_assign.t_id AS "UNITINFOID",
property_assign.t_start_date AS "UNITINFOID",
property_assign.t_end_date AS "UNITINFOID"

FROM property_master , property_unit , unit_info ,tanant_master ,   property_assign where property_assign.unit_info_id=unit_info.unit_info_id  

enter image description here

见上图,所有属性的重复值

我希望aasign属性列表和租户列表在所有属性的proeprty _assign表和tanant_master表中但是当我运行以上查询时它给我错误输出任何想法我该如何解决它?

1 个答案:

答案 0 :(得分:0)

据我所知

- >如果你想从n个表中获取数据,你可以给出n-1个条件。

- >您必须为所有表维护一个公共列,或者为每两个表维护至少一个公共列。

编辑

如果您在所有5个表中都有 r_id 作为公共列,请按如下所示写 where 条件

如果你的表是t1,t2,t3,t4,t5那么

"........WHERE t1.r_id=t2.r_id AND t1.r_id=t3.r_id AND t1.r_id=t4.r_id AND t1.r_id=t5.r_id";

希望这会对你有所帮助。