我有以下选择SQL查询:
SELECT i.instagrammer_id, CONCAT(i.first_name,' ',i.last_name) AS instagrammer_name, FLOOR(DATEDIFF (NOW(), i.date_of_birth)/365) AS age, i.date_of_birth, i.gender, i.email, i.created_date, GROUP_CONCAT(b.name_en) AS industrie_branch, coalesce(c.long_name, '') AS long_name, s.social_id
FROM instagrammer AS i
LEFT JOIN social_login AS s ON s.instagrammer_id = i.instagrammer_id
LEFT JOIN industries_branches b ON FIND_IN_SET(b.industrie_branch_id, i.industrie_branch)
LEFT JOIN country_data c ON c.short_name = i.based_in_country
WHERE i.status IS NOT NULL
GROUP BY i.instagrammer_id
ORDER BY instagrammer_name DESC
并获得此记录:
现在我想从另一个表中加入来自social_id
的匹配,我得到的是SQL结果,但是有一个条件,我需要记录哪个日期是statistic_instagrammer_detail
表的最新日期。
id instagrammer_id followers created_date
** *************** ********* *******************
28 440024439 190 2015-12-10 09:37:03
27 12095962 390 2015-12-10 09:36:03
26 2238046564 544 2015-12-10 09:35:03
25 481959735 780 2015-12-10 09:34:04
24 440024439 151 2015-12-09 09:54:02
23 12095962 210 2015-12-09 09:53:02
22 2238046564 403 2015-12-09 09:52:02
21 481959735 659 2015-12-09 09:51:09
我想在屏幕截图结果中添加followers
:
followers
*********
190
390
544
780
知道怎么做吗?
答案 0 :(得分:0)
您需要在statistic_instagrammer_detail
表上创建一个子查询,该子查询每max(created_date)
返回该表中的instagrammer_id
,然后将statistic_instagrammer_detail
表加入其中以获取{{1} }}
followers
但请注意,您的查询仅在某些配置设置下在mysql中运行,并且违反sql标准,因为它包含选择列表中不在group by子句中的字段,也不会聚合。
答案 1 :(得分:-1)
SELECT i.instagrammer_id, CONCAT(i.first_name,' ',i.last_name) AS instagrammer_name, FLOOR(DATEDIFF (NOW(), i.date_of_birth)/365) AS age, i.date_of_birth, i.gender, i.email, i.created_date, GROUP_CONCAT(b.name_en) AS industrie_branch, coalesce(c.long_name, '') AS long_name, s.social_id,st.followers
FROM instagrammer AS i
LEFT JOIN statistic_instagrammer_detail as st ON i.instagrammer_id=st.instagrammer_id
LEFT JOIN social_login AS s ON s.instagrammer_id = i.instagrammer_id
LEFT JOIN industries_branches b ON FIND_IN_SET(b.industrie_branch_id, i.industrie_branch)
LEFT JOIN country_data c ON c.short_name = i.based_in_country
WHERE i.status IS NOT NULL and st.created_date=(select MAX(created_date) from statistic_instagrammer_detail)
GROUP BY i.instagrammer_id
ORDER BY instagrammer_name DESC