MySQL で 積集合, 和集合, 差集合

積集合

$ x \cap y $

SELECT
  *
FROM
  x
  INNER JOIN
  y
  ON
    x.id=y.id

$ x \cap y \cap z $

SELECT
  *
FROM
  x
  INNER JOIN
  y
  ON
    x.id=y.id
  INNER JOIN
    z
  ON
    x.id=z.id

和集合

$ x \cup y $

SELECT
  *
FROM
  x
LEFT OUTER JOIN
  y
ON
  x.id=y.id
UNION
SELECT
  *
FROM
  x
RIGHT OUTER JOIN
  y
ON
  x.id=y.id

差集合

$ x \setminus y $

SELECT
  *
FROM
  x
  LEFT OUTER JOIN
  y
  ON
    x.id=y.id
WHERE
  y.id IS NULL

$ y \setminus x $

SELECT
  *
FROM
  x
  RIGHT OUTER JOIN
  y
  ON
    x.id=y.id
WHERE
  x.id IS NULL
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.