如何用表示具有这些id的对象在另一个表中替换表中的id?

时间:2016-08-14 11:42:22

标签: sql

两张桌子:

Ukraine, Kyiv, Bandery 26

需要将Dohn地址设置为href="home"而不是1

1 个答案:

答案 0 :(得分:1)

这适用于sql-server,MySQL,MariaDB,PostgreSQL

select u.name, 
concat(a1.country,', ',a1.city,', ',a1.street_address) as address1,
concat(a2.country,', ',a2.city,', ',a2.street_address) as address2
from new.users u
left join old.address a1 on (u.address1 = a1.id)
left join old.address a2 on (u.address2 = a2.id)
where u.name = 'Dohn';

在Oracle上,你必须以不同的方式连接

select u.name, 
a1.country ||', '|| a1.city ||' ,'|| a1.street_address as address1,
concat(concat(concat(concat(a1.country,', '),a1.city),' ,') a1.street_address) as address2
from new.users u
left join old.address a1 on (u.address1 = a1.id)
left join old.address a2 on (u.address2 = a2.id)
where u.name = 'Dohn';

MySql和MariaDB也有CONCAT_WS功能。

SQLite也有像Oracle这样的双重管道。还有一个printf函数:

select u.name, 
(a1.country ||', '|| a1.city ||' ,'|| a1.street_address) as address1,
printf('%s, %s, %s', a2.country, a2.city, a2.street_address) as address2
from new.users u
left join old.address a1 on (u.address1 = a1.id)
left join old.address a2 on (u.address2 = a2.id)
where u.name = 'Dohn';