SQL查询。给出所有sys_language_uid = 1且没有sys_language_uid = 2的PID

时间:2018-12-04 14:40:10

标签: sql typo3

我正在练习TYPO3,我想建立翻译查询。

我需要所有具有sys_language_uid=1但没有翻译sys_language_uid=2的pids

如何使用SQL构建它?

SELECT pids FROM pages_language_overlay 
WHERE sys_language_uid=1 
AND (where sys_language_uid=2 doesnt exist)

THX

3 个答案:

答案 0 :(得分:1)

这里的一种选择是使用聚合:

SELECT pids
FROM pages_language_overlay
GROUP BY pids
HAVING
    COUNT(CASE WHEN sys_language_uid = 1 THEN 1 END) > 0 AND
    COUNT(CASE WHEN sys_language_uid = 2 THEN 1 END) = 0;

答案 1 :(得分:1)

另一种是使用不存在的

(通常最快)

SELECT pids 
FROM pages_language_overlay PLO 
WHERE sys_language_uid = 1 
  and not exists (SELECT 1 
                  FROM pages_language_overlay PLO2
                  WHERE PLO2.sys_language_uid = 2 
                    and PLO.pids = PLO2.pids)

假设PIDS是PK。

或使用左联接...在uid 2上找不到pids匹配时 没有禁食,但提供了更多的数据选择。

SELECT PLO.pids 
FROM pages_language_overlay PLO 
LEFT JOIN pages_language_overlay PLO2
  on PLO.PIDS = PLO2.PIDS
 and PLO2.sys_language_uid = 2
WHERE sys_language_uid = 1 
 and PLO2.PIDS is null

答案 2 :(得分:1)

左联接的另一个选项,但需要为空

select
      PLO.pids
   from
      pages_language_overlay PLO
         LEFT JOIN pages_language_overlay PLO2
            on PLO.pids = PLO2.pids
            AND PLO2.sys_language_uid = 2
   where
          PLO.sys_language_uid = 1
      AND PLO2.sys_language_uid IS NULL