SQL Query不返回预期值

时间:2014-02-06 01:57:10

标签: mysql

我正在为我的客户端和其中一个页面(用PHP和HTML编写)开发数据库,​​我需要显示客户端的所有信息,但是当我运行以下查询时(假设选择)表'系列'中的所有行都返回总共0行。什么时候应该返回表中的所有行

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

1 个答案:

答案 0 :(得分: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的非空值。如果族记录具有每个字段的值,则表明在一个或多个相应表(city,f_d_workers,client_status,lga_locations,facs_locations,indig_status,referrals,community_org)中没有匹配的id字段。< / p>

我通常使用的第一步是当复杂查询不能按照我认为应该工作的方式工作时将内连接转换为外连接,然后只查找丢失的记录。

另一方面,如果一个或多个连接元素是可选的(即,可能没有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

我认为这是你想要的。