连接上的子查询
我正在尝试执行查询来过滤一个表中的行,然后将结果连接到另一个表上,然后过滤掉其他行。
这是我编写的查询:
SELECT *
FROM (SELECT media.id AS id2, media.flagged AS flagged2, media.item_status AS status2
FROM media
WHERE flagged2 != 'nsfw'
AND status2 != 'deleted'
ORDER BY id2 DESC LIMIT 0,5)
JOIN media on info.mid = media.id
WHERE info.topic = 'food_drink'
OR info.topic='cooking'
GROUP BY info.mid
我认为我已经非常接近让查询工作,但我不断收到消息“每个派生表必须有自己的别名”。我已经用谷歌搜索过这个,从我读到的内容来看,我需要为我尝试过的子查询的部分别名,但我仍然无法让它工作。
I'm trying to do a query to filter on rows from one table and then join the results on another table and then filter out additional rows.
Here is the query I've written:
SELECT *
FROM (SELECT media.id AS id2, media.flagged AS flagged2, media.item_status AS status2
FROM media
WHERE flagged2 != 'nsfw'
AND status2 != 'deleted'
ORDER BY id2 DESC LIMIT 0,5)
JOIN media on info.mid = media.id
WHERE info.topic = 'food_drink'
OR info.topic='cooking'
GROUP BY info.mid
I think I'm pretty close to getting the query working but I keep getting the message, "Every derived table must have its own alias." I've Googled this and from what I've read I need to alias parts of the subquery which I've tried but I still can't get it working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
看到
x
了吗?就在括号外面但在连接之前?这就是错误的原因。 FROM 之后和 JOIN 之前括号内的内容是派生表(又名内联视图),MySQL 要求您为其指定表别名。然后,当您从其中引用任何列时,您将使用x.id2
/etc。Use:
See the
x
, just outside the bracket but before the join? That's what the error is about. The stuff inside the brackets after FROM and before JOIN is a derived table (AKA inline view), and MySQL requires that you specify a table alias for it. Then, when you reference any columns from inside it, you'd usex.id2
/etc.