[英]joining row with most recent date in mysql
i'm new to mysql and have been tryn to make this work without any luck. 我是mysql的新手,已经尝试使它正常运行。 i am joining 3 tables and i am tryn to join third table with the row with most recent record as per date.
我正在加入3个表,并且我尝试将第三个表与该日期最新记录的行合并。 here is my mysql query
这是我的mysql查询
SELECT mg.region_name, mu.contact_person,
mu.contact_number, mu.status, mu.company_name, mu.user_type, MAX(mf.follow_up_date), mf.date, mu.user_id
FROM p1006_marketing_group as mg
JOIN p1006_marketing_users as mu ON mg.id = mu.region_id
JOIN ( SELECT user_id_fk,MAX(follow_up_date)
FROM p1006_marketing_follow_up ) as mf
ON mu.user_id = mf.user_id_fk
WHERE mu.user_id_done = "" AND ( mg.id = 3 OR mg.id = 7 OR mg.id = 6 )
GROUP BY mf.user_id_fk
i get the following error 我收到以下错误
Unknown column 'mf.follow_up_date' in 'field list' “字段列表”中的未知列“ mf.follow_up_date”
i am really confused because i do have to column follow_up_date. 我真的很困惑,因为我必须将follow_up_date列。
i would be really greateful for any help possible 我将非常感激任何可能的帮助
You've forgotten to give your MAX(follow_up_date) inside your subquery, an alias 您忘了在子查询中给MAX(follow_up_date)做个别名
Edit your query so it looks like: 编辑查询,使其看起来像:
MAX(follow_up_date) as follow_up_date
Please also note, your query has other issues, such as the select list containing about 8 columns that are not part of an aggregate function, but your group by lists only one column. 另请注意,您的查询还有其他问题,例如选择列表包含约8列,这些列不是聚合函数的一部分,但您的分组依据仅列出一列。 While this might work in MySQL because it can (if configured so) fill in missing group by columns for you, you should really get into the habit of specifying them, to avoid future issues in other employment that uses different database systems
尽管这可能在MySQL中有效,因为它可以(如果配置了)为您填写缺少的分组字段,但是您应该养成指定它们的习惯,以避免将来在使用不同数据库系统的其他工作中遇到问题
You are missing alias for MAX column. 您缺少MAX列的别名。
SELECT
mg.region_name, mu.contact_person, mu.contact_number, mu.status,
mu.company_name, mu.user_type, MAX(mf.follow_up_date), mf.date, mu.user_id
FROM p1006_marketing_group as mg
JOIN p1006_marketing_users as mu ON mg.id = mu.region_id
JOIN ( SELECT user_id_fk,MAX(follow_up_date) 'follow_up_date' FROM p1006_marketing_follow_up ) as mf ON mu.user_id = mf.user_id_fk
WHERE mu.user_id_done = "" AND ( mg.id = 3 OR mg.id = 7 OR mg.id = 6 )
GROUP BY mf.user_id_fk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.