MySql 无法在子查询中访问父级?

发布于 2024-09-25 00:55:17 字数 540 浏览 2 评论 0原文

我收到错误消息,

Unknown column 'm.id' in 'on clause'

我的所有表都有一个名为 id 的字段。它似乎发生在我的子查询中。我在加入中使用 m.id。那么到底是怎么回事呢?我不应该能够访问该变量吗?如何从子查询访问 media.id? 和 not (select 1 部分是否正确?我试图排除该标签。

select m.id from media m 
join tag_name as n0 on n0.title = @a0 
    join media_tag as t0 on t0.media_id=m.id AND t0.tag_id = n0.id
where 1 
AND not (select 1 from tag_name as n1
    join media_tag as t1 on t1.media_id=m.id AND t1.tag_id = n1.id
    where n1.title = @a1) 
order by m.id desc;

I get the error

Unknown column 'm.id' in 'on clause'

all my tables have a field called id. It appears to occur in my subquery. I use m.id in my join. So what the heck is going on? shouldnt i be able to access that var? how do i access media.id from my subquery? Also is the and not (select 1 part right? i am trying to exclude that tag.

select m.id from media m 
join tag_name as n0 on n0.title = @a0 
    join media_tag as t0 on t0.media_id=m.id AND t0.tag_id = n0.id
where 1 
AND not (select 1 from tag_name as n1
    join media_tag as t1 on t1.media_id=m.id AND t1.tag_id = n1.id
    where n1.title = @a1) 
order by m.id desc;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羞稚 2024-10-02 00:55:17

将 t1.media_id=m.id 的检查移至 where 子句,它可以工作(无论如何对我来说):

select m.id from media m 
join tag_name as n0 on n0.title = @a0 
    join media_tag as t0 on t0.media_id=m.id AND t0.tag_id = n0.id
where 1
AND not (select 1 from tag_name as n1
    join media_tag as t1 on t1.tag_id = n1.id
    where t1.media_id=m.id AND n1.title = @a1) 
order by m.id desc;

Move the check of t1.media_id=m.id to the where clause and it works (for me, anyway):

select m.id from media m 
join tag_name as n0 on n0.title = @a0 
    join media_tag as t0 on t0.media_id=m.id AND t0.tag_id = n0.id
where 1
AND not (select 1 from tag_name as n1
    join media_tag as t1 on t1.tag_id = n1.id
    where t1.media_id=m.id AND n1.title = @a1) 
order by m.id desc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文