MySQL中的子查询

时间:2016-09-13 00:07:23

标签: mysql subquery jupyter

我有两个有效的查询,而且我很难弄清楚如何在第一个查询中将第二个子查询作为子查询。

查询1:

select a.personid, a.appt_date,
    case when c.personid is null then 'N' else 'Y' 
    end as follow_up_flag
from (
      select personid, 
        appt_date, 
        lead(appt_date) over (partition by personid order by appt_date) next_appt_date
      from tbl_appt
) a left join tbl_call c on c.personid = a.personid and 
            c.call_date > a.appt_date and 
            (c.call_date < a.next_appt_date or a.next_appt_date is null)
order by a.appt_date 

查询2:

SELECT w.ws_property_id AS WID, w.property_type AS WProp
FROM watershed_property_info w
WHERE EXISTS (SELECT DISTINCT w.property_type
                 FROM st_property_info s            
                 WHERE w.property_type=s.property_type)

结果应该返回st_rental_dates中的d.rental_date,仅用于st_rental_dates中的244个匹配的st_property行,但每次我尝试组合这两个查询时,我得到14,888行。在进行子查询时,我很难理解在哪里放什么。有什么建议吗?

0 个答案:

没有答案