连接上的子查询

发布于 2024-09-07 12:25:49 字数 539 浏览 4 评论 0原文

我正在尝试执行查询来过滤一个表中的行,然后将结果连接到另一个表上,然后过滤掉其他行。

这是我编写的查询:

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 技术交流群。

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

发布评论

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

评论(1

网白 2024-09-14 12:25:49

使用:

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) x
  JOIN media on info.mid = media.id 
 WHERE info.topic = 'food_drink' 
    OR info.topic='cooking' 
GROUP BY info.mid

看到 x 了吗?就在括号外面但在连接之前?这就是错误的原因。 FROM 之后和 JOIN 之前括号内的内容是派生表(又名内联视图),MySQL 要求您为其指定表别名。然后,当您从其中引用任何列时,您将使用 x.id2/etc。

Use:

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) x
  JOIN media on info.mid = media.id 
 WHERE info.topic = 'food_drink' 
    OR info.topic='cooking' 
GROUP BY info.mid

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 use x.id2/etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文