也许尝试自联接,具体取决于您的表结构。
create table config
( GroupID int,
UserID int,
ConfigId int,
ConfigValue varchar(50),
IsDefault int );
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, NULL, 1, 'A', 1);
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, NULL, 2, 'B', 1);
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, NULL, 3, 'C', 1);
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, 1, 1, 'A1', 0);
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, 1, 2, 'B1', 0);
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, 1, 3, 'C1', 0);
insert into config (groupid, userid, configid, configvalue, isdefault)
values (1, 2, 1, 'A2', 0);
select
COALESCE(c1.configid, c2.configid) as configid,
COALESCE(c1.configvalue, c2.configvalue) as configvalue
from config c1 right join config c2
on (c1.groupid = c2.groupid
and c1.configid = c2.configid
and c1.isdefault = 0 and c1.userid = 2)
where c2.isdefault = 1 and c2.groupid = 1;
select
COALESCE(c1.configid, c2.configid) as configid,
COALESCE(c1.configvalue, c2.configvalue) as configvalue
from config c1 right join config c2
on (c1.groupid = c2.groupid
and c1.configid = c2.configid
and c1.isdefault = 0 and c1.userid = 1)
where c2.isdefault = 1 and c2.groupid = 1;
用户 ID = 2 的结果
CONFIGID CONFIGVALUE
1 A2
2 B
3 C
用户 ID = 1 的结果
CONFIGID CONFIGVALUE
1 A1
2 B1
3 C1
我为此构建了一个SQLFiddle,因此您可以自己查看它。