tbl_marketing
db_maid db_date db_customer
1 01-04-2017 xxxx
2 05-04-2017 lll
3 08-04-2017 ggg
tbl_phonecall
db_id db_mid db_due db_nextdate db_pnote
1 2 15-04-2017 16-04-2017 cccc
2 2 17-04-2017 18-04-2017 bbb
3 1 04-05-2017 16-05-2017 ghghh
这是我的查询
选择
select
marketing.*,
phonecall.db_id,
max(phonecall.db_due) db_due,
max(phonecall.db_nextdate) db_nextdate,
phonecall.db_pnote
from tbl_marketing marketing
left outer join tbl_phonecall phonecall
on
phonecall.db_mid=marketing.db_maid
group by marketing.db_maid
我想要的结果是来自tbl_marketing
的所有行,即使tbl_phonecall中存在Null值 BUT NO DUPLICATE RECORDS ,而tbl_phoencall
的最后一行也是db_pnote ,db_due通过tbl_marketing
的db_due以tbl_phonecall
顺序关联到行
我的查询没有给出我想要的确切结果
输出就像那样
# Date Customer dueDate NextDate pNote
2 05-04-2017 lll 17-04-2017 18-04-2017 bbbb
1 01-04-2017 xxxx 04-05-2017 16-05-2017 ccc
3 08-04-2017 ggg ghghh
答案 0 :(得分:1)
您可以使用以下内容:
select marketing.*,
phonecall.db_id,
phonecall.db_due,
phonecall.db_nextdate,
phonecall.db_pnote
from tbl_marketing marketing
left outer join tbl_phonecall phonecall on phonecall.db_mid=marketing.db_maid
and phonecall.db_due=(select max(t2.db_due) from tbl_phonecall t2
where t2.db_mid=marketing.db_maid)
and phonecall.db_nextdate=(select max(t3.db_nextdate) from tbl_phonecall t3
where t3.db_mid=marketing.db_maid)
答案 1 :(得分:0)
您是否希望获得电话中最新日期的所有活动?
SELECT
m.*
,p.db_id
,p.db_due
,p.db_nextdate
,p.db_pnote
FROM tbl_marketing m
LEFT JOIN
(SELECT
db_id
,max(Date) LDate
FROM tbl_phonecall
GROUP BY db_id
) LastDate
ON LastDate.db_mid = m.db_maid
LEFT JOIN tbl_phonecall p
ON m.db_mid = m.db_maid
AND LastDate.LDate = p.Date