例子:
SELECT fbs_household_id,fbh_file_no, fbc_last_name,fbc_first_name
FROM fbs_household
LEFT JOIN (SELECT fbc_household_id, fbc_last_name,fbc_first_name FROM fbs_client WHERE fbc_household_id=fbs_household_id ORDER BY fbc_dob ASC LIMIT 1) t1 ON 1;
fbs_household_id
来自fbs_household
表格,但我收到一条错误消息,提示未知字段。
我可以将查询重写为:
SELECT fbs_household_id,fbh_file_no, fbc_last_name,fbc_first_name
FROM fbs_household
LEFT JOIN fbs_client ON fbs_client_id=(SELECT fbs_client_id
FROM fbs_client
WHERE fbc_household_id=fbs_household_id ORDER BY fbc_dob ASC LIMIT 1)
但这非常慢。有什么办法可以加快速度吗?
我正在尝试获取每个家庭中最年长的成员,以及有关该家庭的一些其他信息(实际查询包含更多列和连接)。
第三种解决方案,也很慢:
SELECT fbs_household_id,fbh_file_no, fbc_last_name,fbc_first_name
FROM fbs_household
LEFT JOIN (SELECT fbc_household_id, fbc_last_name,fbc_first_name
FROM fbs_client
ORDER BY fbc_dob ASC) c1 ON fbc_household_id=fbs_household_id;