![](/img/trans.png)
[英]SQL JOIN: How to echo values of a column of table A with same name as a column of table B?
[英]SQL join table with same column name
我認為這可能對您有幫助。
該查詢使用內部聯接來聯接table1和table2
SELECT T1.name1,T2.name2
FROM `table1` T1
INNER JOIN `table2` T2 ON t2.name1=t1.PrimaryKey;
以您的示例為例:
select * from Table1;
+----+------+------+--------+
| ID | Name | age | Gender |
+----+------+------+--------+
| 1 | Pars | 23 | M |
| 2 | Pepe | 24 | M |
| 3 | Pio | 25 | M |
| 4 | Pak | 26 | F |
+----+------+------+--------+
select * from Table2;
+------+------+------+--------+
| ID | Name | age | Gender |
+------+------+------+--------+
| 1 | Pars | 30 | M |
| 2 | Pepe | 31 | M |
| 3 | Pio | 32 | M |
+------+------+------+--------+
在更新查詢之后:
Update Table1 join Table2 using(ID) set Table1.AGE=Table2.AGE;
結果:
select * from Table1;
+----+------+------+--------+
| ID | Name | age | Gender |
+----+------+------+--------+
| 1 | Pars | 30 | M |
| 2 | Pepe | 31 | M |
| 3 | Pio | 32 | M |
| 4 | Pak | 26 | F |
+----+------+------+--------+
使用左聯接來聯接表。 試試這個代碼:
select a.id ,
coalesce(a.name,b.name) as name
,coalesce(b.age,a.age)as age
,coalesce(a.gender,a.gender)as gender
from table1 as a
left join Table2 as b
on a.id=b.id
也嘗試一下:
update Table1 set Table1.age=Table2.age from table1 inner join table2 on Table1.id=Table2.id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.