Mysql 有点混乱,还有一个音符。在将ProfileRelations表连接回Profile表时,可能存在一个关系,但我特别需要查看字段OrgInd='1',因为其他任何内容都是雇员。我需要查看标记为“仅组织”的记录。 select p.ProfileId, p.O

Mysql 有点混乱,还有一个音符。在将ProfileRelations表连接回Profile表时,可能存在一个关系,但我特别需要查看字段OrgInd='1',因为其他任何内容都是雇员。我需要查看标记为“仅组织”的记录。 select p.ProfileId, p.O,mysql,sql,sql-server,Mysql,Sql,Sql Server,有点混乱,还有一个音符。在将ProfileRelations表连接回Profile表时,可能存在一个关系,但我特别需要查看字段OrgInd='1',因为其他任何内容都是雇员。我需要查看标记为“仅组织”的记录。 select p.ProfileId, p.OrgInd, p.OrgName, p1.OrgInd, p1.ProfileId from profile as p inner join ProfileRelations as pa on p.ProfileId = pa.ProfileI


有点混乱,还有一个音符。在将ProfileRelations表连接回Profile表时,可能存在一个关系,但我特别需要查看字段OrgInd='1',因为其他任何内容都是雇员。我需要查看标记为“仅组织”的记录。
select p.ProfileId, p.OrgInd, p.OrgName, p1.OrgInd, p1.ProfileId
from profile as p inner join
ProfileRelations as pa on p.ProfileId = pa.ProfileID inner join
profile as p1 on p1.ProfileId = pa.RelProfileID
select p.ProfileId, p.OrgInd, p.OrgName, p1.OrgInd, p1.ProfileId
from profile as p 
        inner join
     ProfileRelations as pa 
        on p.ProfileId = pa.ProfileID 
where pa.RelProfileID not in (select ProfileId from profile);
SELECT p.ProfileId, p.OrgInd, p.OrgName, p1.OrgInd, p1.ProfileId
FROM profile as p 
LEFT join ProfileRelations as pa 
   on p.ProfileId = pa.ProfileID 
LEFT join profile as p1 
  on p1.ProfileId = pa.RelProfileID
WHERE PA.ProfileID is null
SELECT p.ProfileId, p.OrgInd, p.OrgName
FROM profile as p 
WHERE NOT EXISTS (SELECT * 
                  FROM ProfileRelations as pa 
                  WHERE p.ProfileId = pa.ProfileID) 
select *
from profile
where ProfileId not in (select ProfileID from ProfileRelations)
  and ProfileId not in (select RelProfileID from ProfileRelations);
select *
from profile p
where not exists
(
  select *
  from ProfileRelations pr
  where p.ProfileId in (pr.ProfileId, pr.RelProfileID)
);
select *
from profile p1
join profile p2 on p2.ProfileID <> p1.ProfileID
where not exists
(
  select *
  from ProfileRelations pr
  where (pr.ProfileId = p1.ProfileId and pr.RelProfileID = p2.ProfileId)
     or (pr.ProfileId = p2.ProfileId and pr.RelProfileID = p1.ProfileId)
);