从不同条件中选择一条记录

huangapple go评论47阅读模式
英文:

Choosing one record ouf of different conditions

问题

根据这两个表:

studyprint

create table studyprint(
    idstudyprint serial not null, 
    empresa varchar(4), 
    remoteaddress varchar(100), 
    primary key(idstudyprint)
);                                                           

insert into studyprint(empresa, remoteaddress) values('TEST', '');
insert into studyprint(empresa, remoteaddress) values('GAM', '');
insert into studyprint(empresa, remoteaddress) values('GAM', '');
insert into studyprint(empresa, remoteaddress) values('TEST', '192.168.0.100');
insert into studyprint(empresa, remoteaddress) values('TEST', '192.168.0.25');

 idstudyprint | empresa | remoteaddress 
--------------+---------+---------------
            1 | TEST    | 
            2 | GAM     | 
            3 | GAM     | 
            4 | TEST    | 192.168.0.100
            5 | TEST    | 192.168.0.25

printprofiles

create table printprofiles(
    idprintprofile serial not null, 
    empresa varchar(4), 
    remoteaddress varchar(100), 
    primary key(idprintprofile)
);

insert into printprofiles(empresa, remoteaddress) values('PDF', '');
insert into printprofiles(empresa, remoteaddress) values('HPR', '');
insert into printprofiles(empresa, remoteaddress) values('GAM', '');
insert into printprofiles(empresa, remoteaddress) values('TEST', '192.168.0.100');
insert into printprofiles(empresa, remoteaddress) values('TEST', '');

 idprintprofile | empresa | remoteaddress 
----------------+---------+---------------
              1 | PDF     | 
              2 | HPR     | 
              3 | GAM     | 
              4 | TEST    | 192.168.0.100
              5 | TEST    | 

我提出的第一个查询是:

select                   
sp.idstudyprint, sp.empresa, pp.idprintprofile, sp.remoteaddress
from studyprint sp
join printprofiles pp on pp.empresa=sp.empresa
where
pp.remoteaddress = sp.remoteaddress or(pp.remoteaddress = '');

其结果为:

 idstudyprint | empresa | idprintprofile | remoteaddress 
--------------+---------+----------------+---------------
            1 | TEST    |              5 | 
            2 | GAM     |              3 | 
            3 | GAM     |              3 | 
            4 | TEST    |              5 | 192.168.0.100
            4 | TEST    |              4 | 192.168.0.100
            5 | TEST    |              5 | 192.168.0.25

由于studyprint中的remoteaddress与printprofiles中的remoteaddress不匹配的情况,对于这些情况,选择必须是匹配的empresa(在我的示例中,'empresa=5'的remoteaddress为空,应该适应所有不匹配的remoteaddress),例如:

 idstudyprint | empresa | idprintprofile | remoteaddress 
--------------+---------+----------------+---------------
            1 | TEST    |              5 | 
            2 | GAM     |              3 | 
            3 | GAM     |              3 | 
            4 | TEST    |              4 | 192.168.0.100
            5 | TEST    |              5 | 192.168.0.25
英文:

Given these two tables:

studyprint:

create table studyprint(
    idstudyprint serial not null, 
    empresa varchar(4), 
    remoteaddress varchar(100), 
    primary key(idstudyprint)
);                                                           

insert into studyprint(empresa, remoteaddress) values('TEST', '');
insert into studyprint(empresa, remoteaddress) values('GAM', '');
insert into studyprint(empresa, remoteaddress) values('GAM', '');
insert into studyprint(empresa, remoteaddress) values('TEST', '192.168.0.100');
insert into studyprint(empresa, remoteaddress) values('TEST', '192.168.0.25');

 idstudyprint | empresa | remoteaddress 
--------------+---------+---------------
            1 | TEST    | 
            2 | GAM     | 
            3 | GAM     | 
            4 | TEST    | 192.168.0.100
            5 | TEST    | 192.168.0.25

printprofiles

create table printprofiles(
    idprintprofile serial not null, 
    empresa varchar(4), 
    remoteaddress varchar(100), 
    primary key(idprintprofile)
);

insert into printprofiles(empresa, remoteaddress) values('PDF', '');
insert into printprofiles(empresa, remoteaddress) values('HPR', '');
insert into printprofiles(empresa, remoteaddress) values('GAM', '');
insert into printprofiles(empresa, remoteaddress) values('TEST', '192.168.0.100');
insert into printprofiles(empresa, remoteaddress) values('TEST', '');

 idprintprofile | empresa | remoteaddress 
----------------+---------+---------------
              1 | PDF     | 
              2 | HPR     | 
              3 | GAM     | 
              4 | TEST    | 192.168.0.100
              5 | TEST    | 

The first query I came up with is this:

select                   
sp.idstudyprint, sp.empresa, pp.idprintprofile, sp.remoteaddress
from studyprint sp
join printprofiles pp on pp.empresa=sp.empresa
where
pp.remoteaddress = sp.remoteaddress or(pp.remoteaddress = '');

Which results in:

 idstudyprint | empresa | idprintprofile | remoteaddress 
--------------+---------+----------------+---------------
            1 | TEST    |              5 | 
            2 | GAM     |              3 | 
            3 | GAM     |              3 | 
            4 | TEST    |              5 | 192.168.0.100
            4 | TEST    |              4 | 192.168.0.100
            5 | TEST    |              5 | 192.168.0.25

As there are cases where a remoteaddress in studyprint doesn't match a remoteaddress in printprofiles, in those cases the selection must be the empresa that matches (in my example, empresa=5 has remoteaddress empty, there should fit all the remoteaddresses that doesn't match), for example:

 idstudyprint | empresa | idprintprofile | remoteaddress 
--------------+---------+----------------+---------------
            1 | TEST    |              5 | 
            2 | GAM     |              3 | 
            3 | GAM     |              3 | 
            4 | TEST    |              4 | 192.168.0.100
            5 | TEST    |              5 | 192.168.0.25

答案1

得分: 1

实际上,你并没有提供详细的说明,但是基于你提供的数据,我写了查询。结果符合你的要求。如果有其他问题,请随时告诉我,我可以帮忙。

我修改了我的查询:

select 
    t1.idstudyprint, 
    t1.empresa, 
    case when t2.idprintprofile is null then t3.idprintprofile else t2.idprintprofile end as idprintprofile, 
    t1.remoteaddress 
from studyprint t1 
left join 
    printprofiles t2 
    on (t1.empresa = t2.empresa and t1.remoteaddress = t2.remoteaddress) 
left join 
    printprofiles t3 
    on t2.idprintprofile is null and t1.empresa = t3.empresa and t3.remoteaddress='' 

--结果: 
idstudyprint | empresa | idprintprofile | remoteaddress |
-------------+---------+----------------+---------------+
           1 | TEST    |              5 |               |
           2 | GAM     |              3 |               |
           3 | GAM     |              3 |               |
           4 | TEST    |              4 | 192.168.0.100 |
           5 | TEST    |              5 | 192.168.0.25  |
英文:

Actually, you didn't write a full detailed explanation, but still, based on the data you wrote, I wrote the query. The result is what you want. Let me know if you have any other questions, I can help.

I changed my query:

select 
    t1.idstudyprint, 
    t1.empresa, 
    case when t2.idprintprofile is null then t3.idprintprofile else t2.idprintprofile end as idprintprofile, 
    t1.remoteaddress 
from studyprint t1 
left join 
    printprofiles t2 
    on (t1.empresa = t2.empresa and t1.remoteaddress = t2.remoteaddress) 
left join 
    printprofiles t3 
    on t2.idprintprofile is null and t1.empresa = t3.empresa and t3.remoteaddress='' 
    
--Result: 
idstudyprint | empresa | idprintprofile | remoteaddress |
-------------+---------+----------------+---------------+
           1 | TEST    |              5 |               |
           2 | GAM     |              3 |               |
           3 | GAM     |              3 |               |
           4 | TEST    |              4 | 192.168.0.100 |
           5 | TEST    |              5 | 192.168.0.25  |

huangapple
  • 本文由 发表于 2023年2月8日 19:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定