JOIN 与 ORDER BY 组合
我想找到表中最早的日期。如何将其放入连接语句中:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 MySQL 是否支持派生表中的 LIMIT,但如果支持,语法应为:
请注意,
order
是保留字,因此您需要正确引用它。要么使用 MySQL 可怕的反引号(见上文),要么 - 如果您将服务器配置为符合标准 - 使用双引号,如下所示:“order”
I'm not sure if MySQL supports a LIMIT In a derived table, but if it does, the syntax should be:
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"
你是这个意思吗?
Is that what you mean?