SELECT ao.ANIMAL_ID, ao.NAME
FROM ANIMAL_OUTS AS ao
LEFT OUTER JOIN ANIMAL_INS AS ai
ON ao.ANIMAL_ID = ai.ANIMAL_ID
WHERE ai.ANIMAL_ID is NULL
ORDER BY ao.ANIMAL_ID
보호일은 있지만 입양일은 없을 수도 있기 때문에 LEFT JOIN OUTER
를 썼다. 날짜는 비교연산자로 비교할 수 있다.
SELECT ai.ANIMAL_ID, ai.NAME
FROM ANIMAL_INS as ai
LEFT JOIN ANIMAL_OUTS as ao
ON ai.ANIMAL_ID = ao.ANIMAL_ID
WHERE ai.DATETIME > ao.DATETIME
ORDER BY ai.DATETIME
SELECT ai.NAME, ai.DATETIME
FROM ANIMAL_INS AS ai
LEFT OUTER JOIN ANIMAL_OUTS AS ao
ON ai.ANIMAL_ID = ao.ANIMAL_ID
WHERE ao.DATETIME is NULL
ORDER BY ai.DATETIME
LIMIT 3
부분적으로 일치하는지 확인할 수 있는 LIKE
구문을 이용했다.
SELECT ao.ANIMAL_ID, ao.ANIMAL_TYPE, ao.NAME
FROM ANIMAL_OUTS AS ao
JOIN ANIMAL_INS AS ai
ON ao.ANIMAL_ID = ai.ANIMAL_ID
WHERE ai.SEX_UPON_INTAKE LIKE 'Intact%'
AND (ao.SEX_UPON_OUTCOME LIKE 'Spayed%' OR ao.SEX_UPON_OUTCOME LIKE 'Neutered%')