I have a problem with this query in MariaDB language. I want to do an intersect with the same field but with two values. The problem is that i can't use the INTERSECT query.
How can I do it?? I have tried with exists and inner join but it still doesn't work.
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')));
INTERSECT was introduced in 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);
As for your query you could leave it as is.
First try it like this, to check you have all the actors. I have to add codactor in case you have actors with same name.
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')
Then add GROUP BY to see which actor are in movies from both directors.
GROUP BY a.codactor, a.nombre
HAVING COUNT(DISTINCT coddirector) = 2
Related
I have the following tables
player
-------------
idPlayer,
name,
age,
position
Team
--------
idTeam,
name,
value
and the intermediate table:
team_has_player
-----------
idPlayer,
idTeam
What I need is to search in "team_has_player" with the variable "idTeam" and select all the players that are the ordered pair, summarizing, search for "idTeam" and show all the "Player" that have their "idPlayer" related to that "idTeam".
Use this SELECT Statement
SELECT
e.nombre,
j.nombre
, j.edad
, j,posicion
FROM `jugador/equipo` je
INNER JOIN Equipo e ON e.idEquipo = je.idEquipo
INNER JOIN jugador j ON j.idJugador = je.idJugador
WHERE je.idEquipo = 1;
It will show you, the name of the Club and the nam,e of the Players plus there Age and Position
If you only want to know the ids from the player related to a team, this could work:
SELECT idPlayer FROM team_has_player WHERE idTeam = 2;
This will return a list with all the players in that team.
If you want to know more information like the names (or other info in the player table) you could do the following:
SELECT p.name, p.position FROM player p
JOIN team_has_player tp
ON tp.idplayer = p.idplayer
WHERE tp.team_idteam = 2;
It is not quite clear, what format you expect as output but you could try the following:
SELECT e.nombre nombre_equipo,
GROUP_CONCAT(j.nombre) jugadores
FROM equipo e
INNER JOIN jugador_equipo je ON je.equipo=e.equipo
INNER JOIN jugador j ON j.idjugador=je.idjugador
GROUP BY e.nombre
I am using the following query to retrieve the number of events per state from 2 tables that are linked by a userID.
SELECT state,COUNT(*) AS num
FROM tableUserInfo
WHERE userID IN (SELECT userID
FROM tableEvents
WHERE conditionOne = 1
AND conditionTwo = 2)
GROUP BY state
This query works correctly. My problem is that not all states have user entries, and I need the query to return 0 for those. I was wondering if there was a method such as joining or using an in clause, that would included a set of all states, making the query return 0 for any that didn't have entries in tableEvents?
Do you have a list of states? If not then this would give a list of all the states your database knows about:
SELECT DISTINCT state FROM tableUserInfo
....and enclosing this in brackets it can be dropped in place in the query below:
SELECT s.state, IFNULL(cnt, 0) AS num
FROM list_of_states s
LEFT JOIN (
SELECT state,COUNT(*) AS cnt
FROM tableUserInfo ui
INNER JOIN tableEvents te
ON ui.userId=te.userId
WHERE conditionOne = 1
AND conditionTwo = 2
GROUP BY state
) u
ON s.state=u.state;
Although in the absence of "list_of_states" it would be more efficient to do this:
SELECT ui.state, SUM(IF(te.userId IS NULL, 0, 1)) AS cnt
FROM tableUserInfo ui
LEFT JOIN tableEvents te
ON ui.userId=te.userId
AND te.conditionOne = 1
AND te.conditionTwo = 2
GROUP BY state;
As #raymond-nijland suggested you can use Left Join to include all states.
SELECT tableUserInfo.state,COUNT(tableUserInfo.*) AS num
FROM tableUserInfo Left Join tableEvents on tableUserInfo.userID = tableEvents.userID
WHERE tableEvents.conditionOne = 1 AND tableEvents.conditionTwo = 2
GROUP BY state
Hi I have the following tables and columns.
movie: ID, title
person: ID, name
involved: personID, movieID
I need to answer the question:
"Which movies have either John Travolta or Uma Thurman, but not both starred in?"
I couldn't figure out how to do this without creating new tables, so I made 2 new tables. And tried to do the full outer join on, where you dont get intersecting results. I found out that you can't do full outer joins in mysql but had to do a left join, unioned with a right join. I tried this but don't get the results I wanted at all. I have been stuck for a while now. Can anyone point me in the right direction?
This is what I have so far.
DROP TABLE IF EXISTS Umatable;
DROP TABLE IF EXISTS Johntable;
CREATE TABLE Umatable(title VARCHAR(500));
CREATE TABLE Johntable(title VARCHAR(500));
INSERT INTO Umatable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "Uma Thurman"
AND p.id = i.personid
AND m.id = i.movieiD;
INSERT INTO Johntable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "John Travolta"
AND p.id = i.personid
AND m.id = i.movieiD;
SELECT *
FROM Umatable
LEFT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
UNION
SELECT *
FROM Umatable
RIGHT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
I would do this using aggregation and having:
select i.movieId
from involved i join
person p
on p.id = i.personId
group by i.movieId
having sum(p.name in ('John Travolta', 'Uma Thurman')) = 1;
A count(*) inside a correlated subquery will work:
select *
from movie m
where 1 = (select count(*)
from involved i
join person p
on p.ID = i.personID
and p.name IN ('John Travolta', 'Uma Thurman')
where i.movieID = m.ID)
SQLFiddle Demo
I'm trying obtain one tabla of cars which have three rows in relations with other three columns (make, model and group) and I only want obtain one car by model.
Here a image of MySQL table:
You will see three rows with same model_id (model_id is foreign key the other table, the other table is called models)
My SQL query for obtain those cars are:
SELECT *
FROM gm_cars AS cars
INNER JOIN gm_cars_makes AS makes
ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models
ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups
ON (cars.group_id = groups.group_id) AND
makes.make_visible = 1
ORDER BY cars.model_id;
but I wish obtain one row for one model, here one example (I have used Photoshop):
Some like: SELECT *, DISTINCT(model_id) FROM cars
If you still want to return all columns, you can create sub-query to return only one Car per Model and then write you query as before:
SELECT * FROM gm_cars AS cars
INNER JOIN (SELECT model_id, MAX(car_Id) AS car_Id FROM gm_cars GROUP BY model_id) AS grp_cars ON grp_cars.car_Id = cars.car_Id
INNER JOIN gm_cars_makes AS makes ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups ON (cars.group_id = groups.group_id) AND makes.make_visible = 1 ORDER BY cars.model_id;
You can also add GROUP BY in you main query, and add aggregation function to all other columns. But it can return you columns from different cars with the same model:
SELECT cars.model_id,
MAX(car_passengers),
MAX(car_suitcases),
....
FROM gm_cars AS cars
INNER JOIN (SELECT model_id, MAX(car_Id) AS car_Id FROM gm_cars GROUP BY model_id) AS grp_cars ON grp_cars.car_Id = cars.car_Id
INNER JOIN gm_cars_makes AS makes ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups ON (cars.group_id = groups.group_id) AND makes.make_visible = 1
GROUP BY cars.model_id
ORDER BY cars.model_id;
SELECT *
FROM gm_cars AS cars
INNER JOIN gm_cars_makes AS makes ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups ON (cars.group_id = groups.group_id)
AND makes.make_visible = 1
group by (model_id)
ORDER BY cars.model_id
is this helpful?
;with cte
AS
(
Select *,row_number() OVER(partition by model_id order by car id) rn from gm_cars
)
select * from cte where rn=1
Best way to do this SELECT?
I've got this tables:
t_department
id
name
t_users
id
name
type
*type can be:
1 SuperUser
2 normalUser
t_department_superuser
(A department can have many superUsers)
-idSuperUser
-idDepartment
t_superuser_normaluser
(A superUser can have many normalusers)
-idSuperUser
-idNormalUser
and finally
t_actions
-id (autonumeric)
-idUser (this can be an id of superUser or normalUser)
-action
Given a department name, for example "mainDepartment"
I need to get all records from t_actions of all normalusers and all superusers of that department
I have this, it works, but I am not an SQL expert (I am using MySQL) and I think it is not the best way to do the select, and t_actions is going to have loads of rows:
SELECT id,idUser,action
FROM t_actions
WHERE (idUser IN (
SELECT DISTINCT t_department_superuser.idSuperUser FROM t_department
RIGHT JOIN t_department_superuser ON t_department_superuser.idDepartment = t_department.id
LEFT JOIN t_superuser_normaluser ON t_superuser_normaluser.idSuperUser = t_department_superuser.idSuperUser
WHERE name='mainDepartment'
UNION ALL
SELECT DISTINCT t_superuser_normaluser.idNormalUser
FROM t_department
RIGHT JOIN t_department_superuser ON t_department_superuser.idDepartment = t_department.id
LEFT JOIN t_superuser_normaluser ON t_superuser_normaluser.idSuperUser = t_department_superuser.idSuperUser
WHERE name='mainDepartment')
ORDER BY id;
Any suggestions to make this better? thank you!!
because you are using left and right joins there will be null records, which is why you need the UNION... you can cut out the UNION with a simple null check
SELECT id, idUser, action
FROM t_actions
WHERE idUser IN
( SELECT DISTINCT COALESCE(tsn.idNormalUser, tds.idSuperUser)
FROM t_department td
RIGHT JOIN t_department_superuser tds ON tds.idDepartment = td.id
LEFT JOIN t_superuser_normaluser tsn ON tsn.idSuperUser = tds.idSuperUser
WHERE td.name='mainDepartment'
)
ORDER BY id;
note i also added alias's to your table names so its easer to write out and read the columns you are trying to select and join on.
EDIT
with the data the only possible way to do it with this table design is like this
SELECT id, idUser, action
FROM t_actions
WHERE idUser IN
((SELECT tds.idSuperUser
FROM t_department td
JOIN t_department_superusers tds ON tds.idDepartment = td.id
WHERE td.name='MAIN')
UNION
(SELECT tsn.idNormalUser
FROM t_department td
JOIN t_department_superusers tds ON tds.idDepartment = td.id
JOIN t_superuser_normaluser tsn ON tsn.idSuperUser = tds.idSuperUser
WHERE td.name='MAIN')
)
ORDER BY id;