表A name user owner

machine1 1 2

machine2 3 4

表B userid username

1 aaa

2 bbb

3 ccc

4 ddd

以上两个表,表A 设备的用户id和所有者id,表B是用户id对应的名称如何用sql语句显示为下面的表

显示为 name user ower

machine1 aaa bbb

machine2 ccc ddd

sql:

SELECT name
, user=(
SELECT username
FROM B
WHERE B.userid = A.user)
, owner=(
SELECT username
FROM B
WHERE B.userid = A.owner)
FROM A

版权声明:本文为博主原创文章,未经博主允许不得转载。

04-28 12:33