在sqlite中加入多个表得到错误的输出
getting wrong output with multiple tables join in sqlite
你好朋友,我有 5 个 table 如下
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
见上图,所有 属性
的值都是重复的
我想要分配 属性 列表和租户列表,它在 proeprty _assign table 和 tanant_master table 中用于所有 属性 但是当我 运行 上面的查询是给我错误的输出不知道我该如何解决它?
据我所知
-> 如果你想从 n 个表中获取数据,你必须给出 n-1 个条件。
-> 你必须为所有表维护一个公用列,或者至少为每两个表维护一个公用列。
编辑 :
如果你有r_id作为所有5个表中的公共列然后写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";
希望对您有所帮助。
你好朋友,我有 5 个 table 如下
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
见上图,所有 属性
的值都是重复的我想要分配 属性 列表和租户列表,它在 proeprty _assign table 和 tanant_master table 中用于所有 属性 但是当我 运行 上面的查询是给我错误的输出不知道我该如何解决它?
据我所知
-> 如果你想从 n 个表中获取数据,你必须给出 n-1 个条件。
-> 你必须为所有表维护一个公用列,或者至少为每两个表维护一个公用列。
编辑 :
如果你有r_id作为所有5个表中的公共列然后写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";
希望对您有所帮助。