JOIN 与 ORDER BY 组合

发布于 2024-12-04 02:03:55 字数 520 浏览 2 评论 0原文

我想找到表中最早的日期。如何将其放入连接语句中:

SELECT date FROM table1 WHERE orderno = 222 ORDER BY date LIMIT 1

Orderno 222 可以在 table1 中拥有 1-* 多行,这就是我使用 LIMIT 1 的原因

我有这些表:

订单 订单行 ProductionDate

订单可以有 1-* ProductionDates,因此当我加入 ProductionDate 时,我想找到最早的日期。

所以我对 sql 语句的猜测是这样的:

SELECT * FROM Order
LEFT JOIN (
IN SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1)

但这不起作用。我想知道我应该改变什么才能让它工作?

I want to find the earliest date in a table. How do I put this into a join statement:

SELECT date FROM table1 WHERE orderno = 222 ORDER BY date LIMIT 1

Orderno 222 can have 1-* many rows in table1 that's why I'm using LIMIT 1

I have table these tables:

Order
OrderLine
ProductionDate

Order can have 1-* ProductionDates so when i do my join of ProductionDate i want to find the earliest date.

So my guess of a sql statement would be something like:

SELECT * FROM Order
LEFT JOIN (
IN SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1)

but this doesn't work. And i would like to know what i should change to get it to work?

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

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

发布评论

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

评论(2

人疚 2024-12-11 02:03:55

我不确定 MySQL 是否支持派生表中的 LIMIT,但如果支持,语法应为:

SELECT ord.*
FROM `Order` ord
  LEFT JOIN (SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1) t
       ON t.date = ord.date

请注意,order 是保留字,因此您需要正确引用它。要么使用 MySQL 可怕的反引号(见上文),要么 - 如果您将服务器配置为符合标准 - 使用双引号,如下所示:“order”

I'm not sure if MySQL supports a LIMIT In a derived table, but if it does, the syntax should be:

SELECT ord.*
FROM `Order` ord
  LEFT JOIN (SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1) t
       ON t.date = ord.date

Note that order is a reserved word, so you will need to quote it properly. Either using MySQL dreaded backticks (see above) or - if you configured your server to be standard's compliant - using double quotest like this: "order"

末が日狂欢 2024-12-11 02:03:55
SELECT `date` 
FROM `table1` 
INNER JOIN `table1`.`date` ON `table2`.`date`
ORDER BY date ASC LIMIT 1

你是这个意思吗?

SELECT `date` 
FROM `table1` 
INNER JOIN `table1`.`date` ON `table2`.`date`
ORDER BY date ASC LIMIT 1

Is that what you mean?

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