2014-02-06 152 views
0

我正在爲我的客戶端和頁面之一(用PHP和HTML編寫)的數據庫工作,我需要在其客戶端上顯示所有信息,但是當我運行下面的查詢(假設選擇'family'表中的所有行),它返回總共0行。當它應該返回所有行的表中的SQL查詢不返回預期值

SELECT 
families.id AS fam_id, 
families.last_name AS fam_surname, 
families.address_1 AS fam_address_1, 
families.address_2 AS fam_address_2, 
families.city_id AS fam_city, 
families.phone AS fam_phone, 
families.mobile AS fam_mobile, 
families.email AS fam_email, 
families.f_d_worker_1 AS fam_fdw_1, 
families.f_d_worker_2 AS fam_fdw_2, 
families.status_id AS fam_status_id, 
families.trans_date AS fam_trans_date, 
families.entry_date AS fam_entry_date, 
families.exit_date AS fam_exit_date, 
families.eligible_date AS fam_eligible_date, 
families.active_date AS fam_active_date, 
families.lga_loc_id AS fam_lga_id, 
families.facs_loc_id AS fam_facs_id, 
families.ind_status_id AS fam_indig_id, 
families.referral_id AS fam_ref_id, 
families.active_status AS fam_act_status, 
families.comm_org_id AS fam_com_org, 
city.id AS city_id, 
city.name AS city_name, 
city.state_id AS city_state, 
city.post_code AS post_code, 
states.id AS state_id, 
states.long_name AS state_name, 
states.abbrev AS state_abbrev, 
client_status.id AS client_stat_id, 
client_status.name AS client_stat_name, 
community_org.id AS com_org_id, 
community_org.name AS com_org_name, 
facs_location.id AS facs_id, 
facs_location.name AS facs_name, 
lga_location.id AS lga_id, 
lga_location.name as lga_name, 
indig_status.id AS indig_id, 
indig_status.name AS indig_name, 
referrals.id AS ref_id, 
referrals.name AS ref_name, 
f_d_workers.id AS fdw_id, 
f_d_workers.first_name AS fdw_first_name, 
f_d_workers.last_name AS fdw_last_name, 
client_status.id AS client_id, 
client_status.name AS client_name 
FROM 
`families`, 
`city`, 
`client_status`, 
`community_org`, 
`facs_location`, 
`f_d_workers`, 
`indig_status`, 
`lga_location`, 
`referrals`, 
`states` 
WHERE 
families.city_id = city.id AND 
families.f_d_worker_1 = f_d_workers.id AND 
families.f_d_worker_2 = f_d_workers.id AND 
families.status_id = client_status.id AND 
families.lga_loc_id = lga_location.id AND 
families.facs_loc_id = facs_location.id AND 
families.ind_status_id = indig_status.id AND 
families.referral_id = referrals.id AND 
families.comm_org_id = community_org.id 
+0

你真的應該使用'INNER JOIN'來做這個... – hichris123

+0

@ hichris123我對INNER JOIN函數沒有任何經驗,並且有一點研究我沒有看到它適合我的情況。請您詳細說明如何使用它? –

回答

1

沒有看到你的架構或數據,我會猜測,一個或多個查詢中的聯接不工作的方式,你認爲這是去上班。例如,查詢意味着每個家庭記錄對於city_id,f_d_worker_1,f_d_worker_2,status_id,lga_loc_id,facs_loc_id,ind_status_id,referral_id和comm_org_id都將具有非空值。如果家庭記錄具有這些字段中的每個字段的值,則看起來在一個或多個對應表(城市,f_d_workers,client_status,lga_locations,facs_locations,indig_status,引用,community_org)中沒有匹配的id字段。

當我認爲應該工作的複雜查詢無法正常工作時,我通常會使用的第一步是將內部聯接轉換爲外部聯接,然後只查找缺失的記錄。

另一方面,如果您的一個或多個聯接元素是可選的(即可能沒有f_d_worker_2 id),那麼您應該在查詢本身中使用外部聯接。我希望這有幫助。

修訂

SELECT 
families.id AS fam_id, 
families.last_name AS fam_surname, 
families.address_1 AS fam_address_1, 
families.address_2 AS fam_address_2, 
families.city_id AS fam_city, 
families.phone AS fam_phone, 
families.mobile AS fam_mobile, 
families.email AS fam_email, 
families.f_d_worker_1 AS fam_fdw_1, 
families.f_d_worker_2 AS fam_fdw_2, 
families.status_id AS fam_status_id, 
families.trans_date AS fam_trans_date, 
families.entry_date AS fam_entry_date, 
families.exit_date AS fam_exit_date, 
families.eligible_date AS fam_eligible_date, 
families.active_date AS fam_active_date, 
families.lga_loc_id AS fam_lga_id, 
families.facs_loc_id AS fam_facs_id, 
families.ind_status_id AS fam_indig_id, 
families.referral_id AS fam_ref_id, 
families.active_status AS fam_act_status, 
families.comm_org_id AS fam_com_org, 
city.id AS city_id, 
city.name AS city_name, 
city.state_id AS city_state, 
city.post_code AS post_code, 
states.id AS state_id, 
states.long_name AS state_name, 
states.abbrev AS state_abbrev, 
client_status.id AS client_stat_id, 
client_status.name AS client_stat_name, 
community_org.id AS com_org_id, 
community_org.name AS com_org_name, 
facs_location.id AS facs_id, 
facs_location.name AS facs_name, 
lga_location.id AS lga_id, 
lga_location.name as lga_name, 
indig_status.id AS indig_id, 
indig_status.name AS indig_name, 
referrals.id AS ref_id, 
referrals.name AS ref_name, 
workers1.id AS fdw1_id, 
workers1.first_name AS fdw1_first_name, 
workers1.last_name AS fdw1_last_name, 
workers2.id AS fdw2_id, 
workers2.first_name AS fdw2_first_name, 
workers2.last_name AS fdw2_last_name, 
client_status.id AS client_id, 
client_status.name AS client_name 
FROM 
`families` 
LEFT OUTER JOIN `city` ON families.city_id = city.id 
LEFT OUTER JOIN `client_status` ON families.status_id = client_status.id 
LEFT OUTER JOIN `community_org` ON families.comm_org_id = community_org.id 
LEFT OUTER JOIN `facs_location` ON families.facs_loc_id = facs_location.id 
LEFT OUTER JOIN `f_d_workers` AS workers1 ON families.f_d_worker_1 = workers1.id 
LEFT OUTER JOIN `f_d_workers` AS workers2 ON families.f_d_worker_2 = workers2.id 
LEFT OUTER JOIN `indig_status` ON families.ind_status_id = indig_status.id 
LEFT OUTER JOIN `lga_location` ON families.lga_loc_id = lga_location.id 
LEFT OUTER JOIN `referrals` ON families.referral_id = referrals.id 
LEFT OUTER JOIN `states` ON city.state_id = states.id 

我想這是你想要知道的。

+0

這是架構和查詢的SQL小提琴。我希望它有幫助。我對SQL有一定的經驗,但是這些更大的查詢似乎在一點點地做我的頭。 http://sqlfiddle.com/#!2/b48d1 –

+0

看起來,你正在爲城市,f_d_worker_1,f_d_worker_2等家庭記錄填入零。在城市,f_d_workers等沒有對應的條目,id =這就是爲什麼查詢沒有返回任何東西。如果這些值應該是可選的,那麼使連接外部連接而不是內部連接。如果它們不是可選的,則用有效數據填充家庭記錄(即查找表中存在的id值)。 – MichaelMilom

+0

這確實在一個階段讓我想到了,但是我沒有內外聯接區別的經驗,所以這只是一個簡短的想法。我會做一點研究,並希望拿出正確的查詢:) –