MariaDB 中的 INTERSECT 查询
INTERSECT query in MariaDB
我在使用 MariaDB 语言进行查询时遇到问题。我想与同一个字段相交,但有两个值。问题是我不能使用 INTERSECT 查询。
我该怎么做??我已尝试使用 exists 和 inner join,但它仍然不起作用。
SELECT nombre
FROM actores
WHERE codactor IN ( SELECT actor
FROM participacion
WHERE (titulo,año) IN (SELECT titulo, año
FROM peliculas
WHERE director IN (
SELECT coddirector
FROM directores d
WHERE d.nombre='Alejandro'
AND d.apellido='Amenabar')))
INTERSECT
SELECT nombre
FROM actores
WHERE codactor IN ( SELECT actor
FROM participacion
WHERE (titulo,año) IN (SELECT titulo,año
FROM peliculas
WHERE director in (
SELECT coddirector
from directores p
WHERE p.nombre='Pedro'
AND p.apellido='Almodobar')));
先这样试一下,看看你有没有所有的演员。如果您有同名演员,我必须添加 Codactor。
SELECT a.codactor, a.nombre -- add ', *' to see all columns and test query is ok.
FROM actores a
JOIN participacion p
ON a.codactor = p.actor
JOIN peliculas m
ON p.titulo = m.titulo
AND p.ano = m.ano
JOIN directores d
ON p.director = d.coddirector
WHERE (d.nombre = 'Alejandro' and d.apellido = 'Amenabar')
OR (d.nombre = 'Pedro' and d.apellido = 'Almodobar')
然后添加 GROUP BY
以查看两位导演的电影中有哪些演员。
GROUP BY a.codactor, a.nombre
HAVING COUNT(DISTINCT coddirector) = 2
INTERSECT
是在 MariaDB 10.3.0 中引入的。
INTERSECT
The result of an intersect is the intersection of right and left SELECT results, i.e. only records that are present in both result sets will be included in the result of the operation.
(SELECT e_name AS name, email FROM employees)
INTERSECT
(SELECT c_name AS name, email FROM customers);
至于您的查询,您可以保持原样。
我在使用 MariaDB 语言进行查询时遇到问题。我想与同一个字段相交,但有两个值。问题是我不能使用 INTERSECT 查询。
我该怎么做??我已尝试使用 exists 和 inner join,但它仍然不起作用。
SELECT nombre
FROM actores
WHERE codactor IN ( SELECT actor
FROM participacion
WHERE (titulo,año) IN (SELECT titulo, año
FROM peliculas
WHERE director IN (
SELECT coddirector
FROM directores d
WHERE d.nombre='Alejandro'
AND d.apellido='Amenabar')))
INTERSECT
SELECT nombre
FROM actores
WHERE codactor IN ( SELECT actor
FROM participacion
WHERE (titulo,año) IN (SELECT titulo,año
FROM peliculas
WHERE director in (
SELECT coddirector
from directores p
WHERE p.nombre='Pedro'
AND p.apellido='Almodobar')));
先这样试一下,看看你有没有所有的演员。如果您有同名演员,我必须添加 Codactor。
SELECT a.codactor, a.nombre -- add ', *' to see all columns and test query is ok.
FROM actores a
JOIN participacion p
ON a.codactor = p.actor
JOIN peliculas m
ON p.titulo = m.titulo
AND p.ano = m.ano
JOIN directores d
ON p.director = d.coddirector
WHERE (d.nombre = 'Alejandro' and d.apellido = 'Amenabar')
OR (d.nombre = 'Pedro' and d.apellido = 'Almodobar')
然后添加 GROUP BY
以查看两位导演的电影中有哪些演员。
GROUP BY a.codactor, a.nombre
HAVING COUNT(DISTINCT coddirector) = 2
INTERSECT
是在 MariaDB 10.3.0 中引入的。
INTERSECT
The result of an intersect is the intersection of right and left SELECT results, i.e. only records that are present in both result sets will be included in the result of the operation.
(SELECT e_name AS name, email FROM employees) INTERSECT (SELECT c_name AS name, email FROM customers);
至于您的查询,您可以保持原样。