2013-12-11 2 views
0

값이있는 두 개의 테이블과 사용자가있는 두 개의 테이블이 있습니다.두 테이블의 날짜를 비교하는 위치 업데이트

Table_1.valuea으로 업데이트하고 싶습니다. 여기 내 Table_1.date < user_table.date은 주어진 user_id입니다. 특정 사용자가 where table1.user_id = 1 또는 ID를 추가하려면

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

:

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 | 

답변

2

이 시도.

는 바이올린에 그것을 참조 : 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 
+0

감사합니다. 나는'Table_3'의 값을'SET'하려고 시도했다.'Column not found : 1054 Unknown column Table_1.user_id on on clause'을 던졌습니다. 나는'UPDATE table_1, table_3 inner join user_table을 (table1.user_id = user_table.user_id 및 table1.date Wistar

+0

내 편집 됨 대답. 그것이 맞는 지보십시오. –

+0

당신의 대답은 옳았습니다, 나는 내부 결합이 끝난 후에 Table_3을 넣을 필요가있었습니다. 감사합니다 – Wistar

0
update Table_1,user_table 
set value='a' 
where Table_1.user_id = user_table.user_id 
    and Table_1.date < user_table.date 
관련 문제