sql >> Database >  >> RDS >> Mysql

Combineer twee mysql-query's in één

U kunt niet ORDER BY in je eerste SELECT en dan UNION het.

Bewerken
U kunt echter

zoals in de MySQL UNION-documentatie

(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

Wat vervolgens uw SQL

(SELECT clicks FROM clicksTable WHERE clicks > 199 ORDER BY clicks ASC LIMIT 1)
UNION
(SELECT clicks FROM clicksTable ORDER BY clicks DESC LIMIT 1);

Bewerk 2
Terugkeren in een array

SELECT (SELECT clicks 
        FROM clicksTable 
        WHERE clicks > 199 
        ORDER BY clicks ASC 
        LIMIT 1) AS NextClick,
       (SELECT clicks 
        FROM clicksTable 
        ORDER BY clicks DESC 
        LIMIT 1) AS TopClick;


  1. MySQL-datumvergelijking

  2. Wat doet 'COLLATE SQL_Latin1_General_CP1_CI_AS'?

  3. JSON_INSERT() versus JSON_SET() versus JSON_REPLACE() in SQLite

  4. MySQL naar Oracle syntaxisfout (limiet / offset / update)