sql >> Database teknologi >  >> RDS >> Mysql

MySQL-gruppe ved med rækkefølge/prioritering af en anden kolonne

Du kan bruge det MAX eksempel, du skulle bare "falske det". Se her:http://sqlfiddle.com/#!2/58688/5

SELECT *
FROM test
JOIN (SELECT 'west' AS direction, 4 AS weight
      UNION
      SELECT 'north',3
      UNION
      SELECT 'south',2
      UNION
      SELECT 'east',1) AS priority
  ON priority.direction = test.direction
JOIN (
      SELECT route, MAX(weight) AS weight
      FROM test
      JOIN (SELECT 'west' AS direction, 4 AS weight
            UNION
            SELECT 'north',3
            UNION
            SELECT 'south',2
            UNION
            SELECT 'east',1) AS priority
        ON priority.direction = test.direction
      GROUP BY route
) AS t1
  ON t1.route = test.route
    AND t1.weight = priority.weight


  1. Sådan tilføjes en separator til en sammenkædet streng i MySQL – CONCAT_WS()

  2. Introduktion til HDFS | Hvad er HDFS, og hvordan virker det?

  3. Meteor med mysql

  4. Fordele ved at bruge Microsoft Access-skabeloner