I would like to know if it is possible to combine these two sql queries in 1 unique query.
SELECT DISTINCT Pseudo FROM Games
WHERE Time_Format IS NOT NULL
AND Score IS NOT NULL
AND Result = 'Win' ORDER BY Date DESC
SELECT ID_Game,Time_Format,Time,Score,Date
FROM Games
WHERE Time_Format IS NOT NULL
AND Score IS NOT NULL
AND Result = 'Win'
AND Pseudo = to the previous result of the other request
ORDER BY Time ASC,Score DESC LIMIT 1
Here is an example of data in the Games table
INSERT INTO Games(ID_Game, Pseudo, Time_Format,Time, Score,Statut, Result , Date, Date_Fin) VALUES (1,'bob','00:22',22,125,'completed','Win','2022-06-22 13:47:43','2022-06-22 13:47:52'),
(1,'bob2','00:52',52,369,'completed','Lose','2022-06-22 13:40:22','2022-06-22 13:40:22'),
(1,'bob3','00:36',36,650,'completed','Win','2022-06-22 13:22:22','2022-06-22 13:22:22'),
(1,'bob4','00:40',40,378,'completed','Win','2022-06-22 13:12:22','2022-06-22 13:12:22');
What I would like to know is to get the best times and scores of the won games but I don't want two identical nicknames in the result
Thanks in advance for your help :)
You should use CTE or subquery . I prefer CTE:
WITH cte AS (
SELECT DISTINCT Pseudo
FROM Games
WHERE Time_Format IS NOT NULL
AND Score IS NOT NULL
AND Result = 'Win'
--ORDER BY Date DESC
)
SELECT ID_Game,Time_Format,Time,Score,Date
FROM Games
WHERE Time_Format IS NOT NULL
AND Score IS NOT NULL
AND Resultat = 'Win'
AND Pseudo = (SELECT pseudo FROM cte)
ORDER BY Time ASC, Score DESC
LIMIT 1;
, but if CTE is not an option there is a version with subquery:
SELECT ID_Game,Time_Format,Time,Score,Date
FROM Games
WHERE Time_Format IS NOT NULL
AND Score IS NOT NULL
AND Resultat = 'Win'
AND Pseudo = (
SELECT DISTINCT Pseudo
FROM Games
WHERE Time_Format IS NOT NULL
AND Score IS NOT NULL
AND Result = 'Win'
--ORDER BY Date DESC
)
ORDER BY Time ASC, Score DESC
LIMIT 1;
If CTE returns multiple values just replace AND Pseudo = (SELECT pseudo FROM cte)
with AND Pseudo IN (SELECT pseudo FROM cte)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.