SELECT hh_podcast_channel.id
FROM hh_podcast_channel
inner JOIN (
SELECT hh_podcast_episodes.podcast_id,
ROW_NUMBER() OVER(PARTITION BY hh_podcast_episodes.id ORDER BY hh_podcast_episodes.published_on) as rn
FROM hh_podcast_episodes
) as t2
ON hh_podcast_channel.id = hh_podcast_episodes.podcast_id
having t2.rn = 1
#1054 – Unknown column ‘hh_podcast_episodes.podcast_id’ in ‘on clause’
3
Answers
You need to use the table alias (t2) in the JOIN condition
Why you even use the subquery in join. Use the
row_number
in main query and then use it in the subquery for simplicity as follows:I would suggest:
This fixes your specific problem which is that your table aliases are confused. In addition:
HAVING
withWHERE
. Although both happen to work in MySQL, the use ofHAVING
is misleading because it implies aggregation.pe.*
, relying on the optimizer to choose the columns in the subquery. I just find that that is more convenient, so if I decide to include another column, I don’t have to make changes in multiple places.I also suspect that you want
SELECT DISTINCT
, but your question doesn’t provide sample data and desired results.