Oracle Db 按两列分组
Oracle Db Group By Two Columns
我想按 2 列分组,我的第一个 table 是:
用户
ID Name Surname
1 joe New
2 Ala Bla
3 Cat Kra
4 Zoo Fles
5 Kat Glo
工作:
ID BOSSID1 BOSSID2
1 1 2
2 2 3
3 2 1
4 2 5
5 3 5
而且我想看到 table "Work" 中的所有 Boss。
例如:
1 joe New
2 Ala Bla
3 Cat Kra
5 Kat Glo
试试这个:
select id, concat(name, ' ', surname) from
(
select id, name, surname from users
where id in
(
select distinct bossid1 from work
) OR id in
(
select distinct bossid2 from work
)
)
试试这个:
SELECT DISTINCT u.* FROM USERS u INNER JOIN WORK w
ON (u.id = w.bossid1 OR u.id = w.bossid2)
你可以 union
然后加入 users
table
SELECT CONCAT_WS(' ', U.Name, U.Surname)
FROM Users U
JOIN
(SELECT BOSSID1 as BossID
FROM WORK
UNION
SELECT BOSSID2 as BossID
FROM WORK
) T
on U.ID = T.BossID
我想按 2 列分组,我的第一个 table 是:
用户
ID Name Surname
1 joe New
2 Ala Bla
3 Cat Kra
4 Zoo Fles
5 Kat Glo
工作:
ID BOSSID1 BOSSID2
1 1 2
2 2 3
3 2 1
4 2 5
5 3 5
而且我想看到 table "Work" 中的所有 Boss。
例如:
1 joe New
2 Ala Bla
3 Cat Kra
5 Kat Glo
试试这个:
select id, concat(name, ' ', surname) from
(
select id, name, surname from users
where id in
(
select distinct bossid1 from work
) OR id in
(
select distinct bossid2 from work
)
)
试试这个:
SELECT DISTINCT u.* FROM USERS u INNER JOIN WORK w
ON (u.id = w.bossid1 OR u.id = w.bossid2)
你可以 union
然后加入 users
table
SELECT CONCAT_WS(' ', U.Name, U.Surname)
FROM Users U
JOIN
(SELECT BOSSID1 as BossID
FROM WORK
UNION
SELECT BOSSID2 as BossID
FROM WORK
) T
on U.ID = T.BossID