关于查询的Mysql双数据

时间:2016-01-07 09:48:50

标签: mysql

我有2个表的数据,比如这个

Table 1
no|   name  |  address |
1 | alex    |  br st   | 
2 | ujang   |  cilala  |
3 | adu     |  lind st |
4 | ujang   |  bilila  |
5 | ujang   |  gea     |

Table 2
no|   name  |  address |
1 | alex    |          |
2 | ujang   |          |
3 | adu     |          |
4 | adu     |          |

我的查询就像这样

UPDATE TABLE1 a 
JOIN TABLE2 b ON a.name = b.name
SET a.address = b.address

我想要的结果

 Table 2
    no|   name  |  address |
    1 | alex    |  br st   |
    2 | ujang   |  cilala  |
    3 | adu     |  lind st |
    4 | adu     |  lind st |

我无话可说,我冻结了。

3 个答案:

答案 0 :(得分:2)

以下查询使用临时表更新TABLE2,如果TABLE1可能多次出现no,则该临时表仅包含来自nameTABLE1值最小的记录UPDATE TABLE2 a INNER JOIN ( SELECT t1.no, t1.name, t1.address FROM TABLE1 t1 INNER JOIN ( SELECT name, MIN(no) AS no FROM TABLE1 GROUP BY name ) t2 ON t1.name = t2.name AND t1.no = t2.no ) b ON a.name = b.name SET a.address = b.address

$('.id_1 .favorite').removeClass('favourite').addClass('title');

答案 1 :(得分:0)

正如你所提到的,我认为你想要tabel1中table2中的所有地址,所以如果你想要,请用这个更改查询:

UPDATE TABLE1 a JOIN TABLE2 b ON a.name = b.name SET b.address = a.address

答案 2 :(得分:0)

像这样使用。

UPDATE table2 
SET 
table2.address = table1.address

FROM table1, table2 
WHERE table1.name = table2.name