MySql 左连接在几个regs上

发布于 2024-08-27 23:09:40 字数 1168 浏览 8 评论 0原文

我有这个 table1

idproduct(PK) | date_to_go
1               2010-01-18
2               2010-02-01
3               2010-02-21
4               2010-02-03

和另一个 table2 控制 date_to_go 更新

id | idproduct(FK) | prev_date_to_go | date_to_go | update_date
1    1               2010-01-01        2010-01-05   2009-12-01
2    1               2010-01-05        2010-01-10   2009-12-20
3    1               2010-01-10        2010-01-18   2009-12-20
4    3               2010-01-20        2010-02-03   2010-01-05

所以,在这个例子中,对于 table1.idproduct #1 2010-01-18 是实际的 date_to_go 和 2010-01-01 (table2.prev_date_to_go,第一个 reg)是原始的预计出发日期。

使用此查询

select v.idproduct, v.date_to_go, p.prev_date_to_go original_date_to_go 
from table1 v 
left join produto_datas p on p.idproduto = v.idproduto
group by (v.idproduto)
order by v.idproduto

我可以假设original_date_to_go将是table2的第一个相关reg吗?

idproduct | date_to_go | original_date_to_go 
1           2010-01-18   2010-01-01
2           2010-02-01   NULL
3           2010-02-21   2010-01-20 
4           2010-02-03   NULL 

I have this table1

idproduct(PK) | date_to_go
1               2010-01-18
2               2010-02-01
3               2010-02-21
4               2010-02-03

and this other table2 that controls date_to_go updates

id | idproduct(FK) | prev_date_to_go | date_to_go | update_date
1    1               2010-01-01        2010-01-05   2009-12-01
2    1               2010-01-05        2010-01-10   2009-12-20
3    1               2010-01-10        2010-01-18   2009-12-20
4    3               2010-01-20        2010-02-03   2010-01-05

So, in this example, for table1.idproduct #1 2010-01-18 is the actual date_to_go and 2010-01-01 (table2.prev_date_to_go, first reg) is the original date_to_go .

using this query

select v.idproduct, v.date_to_go, p.prev_date_to_go original_date_to_go 
from table1 v 
left join produto_datas p on p.idproduto = v.idproduto
group by (v.idproduto)
order by v.idproduto

can I assume that original_date_to_go will be the first related reg of table2?

idproduct | date_to_go | original_date_to_go 
1           2010-01-18   2010-01-01
2           2010-02-01   NULL
3           2010-02-21   2010-01-20 
4           2010-02-03   NULL 

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

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

发布评论

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

评论(2

格子衫的從容 2024-09-03 23:09:41

不,您不能假设您始终会从第一行获取 prev_date_to_go 的值。当您选择的列未在 GROUP BY 中列出时,您可以返回的值可能是任何行中的值。请参阅 MySQL 文档 GROUP BY 和 HAVING with Hidden专栏了解更多信息:

使用此功能时,每个组中的所有行对于 GROUP BY 部分中省略的列应具有相同的值。服务器可以自由地从组中返回任何值,因此结果是不确定的,除非所有值都相同。

No, you cannot assume that you will always get the value of prev_date_to_go from the first row. When you select a column that isn't listed in your GROUP BY, the values you could get back could be the value from any row. See the MySQL documentation GROUP BY and HAVING with Hidden Columns for more information:

When using this feature, all rows in each group should have the same values for the columns that are ommitted from the GROUP BY part. The server is free to return any value from the group, so the results are indeterminate unless all values are the same.

花间憩 2024-09-03 23:09:41

好吧,我使用了子查询。

select v.idproduct, v.date_to_go,  (select p.prev_date_to_go from table2 p where p.idproduct=v.idproduct order by idtable2 asc limit 1)  original_date_to_go from table1 v group by (v.idproduct) order by v.idproduct

Well, I used a subquery.

select v.idproduct, v.date_to_go,  (select p.prev_date_to_go from table2 p where p.idproduct=v.idproduct order by idtable2 asc limit 1)  original_date_to_go from table1 v group by (v.idproduct) order by v.idproduct
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文