gpt4 book ai didi

mysql - 更新比较两个表中的日期的位置

转载 作者:行者123 更新时间:2023-11-29 08:16:21 24 4
gpt4 key购买 nike

我有两张表,一张包含值,另一张包含用户。

我想更新我的 Table_1.valuea我在哪里Table_1.date < user_table.date对于给定的user_id .

Table_1

|user_id | value | date |
|--------|-------|------------|
| 1 | f | 2013-12-11 |
|--------|-------|------------|
| 2 | k | 2013-12-05 |
|--------|-------|------------|
| 3 | l | 2013-12-01 |
|--------|-------|------------|
| 4 | n | 2013-11-09 |
|--------|-------|------------|
| 4 | a | 2012-10-11 |
|--------|-------|------------|
| 2 | v | 2013-11-07 |
|--------|-------|------------|
| 1 | o | 2013-12-10 |
|--------|-------|------------|
| 3 | p | 2013-11-15 |

user_table

|user_id | date |
|--------|------------|
| 1 | 2013-12-15 |
|--------|------------|
| 2 | 2013-11-03 |
|--------|------------|
| 3 | 2013-12-11 |
|--------|------------|
| 4 | 2013-12-09 |

最佳答案

试试这个:

update table1 inner join user_table
on (table1.user_id = user_table.user_id
and table1.date < user_table.date)
set table1.value = 'a'

如果您想添加特定用户,请添加 where table1.user_id = 1 或任何 ID。

在 fiddle 上查看:http://sqlfiddle.com/#!2/e0895/1

编辑

随着需求的变化(多一张表),它将是:

UPDATE table_1 inner join table_3 
on (table_1.date = table_3.date)
inner join user_table on
(table1.user_id = user_table.user_id
and table1.date < user_table.date )
set table_1.value = table_3.value

关于mysql - 更新比较两个表中的日期的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com