Aquí está mi mesa y los datos contenidos en ella:¿Qué está pasando al usar DISTINCT?
Table: first
+----------+------+
| first_id | data |
+----------+------+
| 1 | 5 |
| 2 | 6 |
| 3 | 7 |
| 4 | 6 |
| 5 | 7 |
| 6 | 5 |
| 7 | 7 |
| 8 | 6 |
| 9 | 5 |
| 10 | 7 |
+----------+------+
Table: second
+-----------+----------+----------+
| second_id | first_id | third_id |
+-----------+----------+----------+
| 1 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 3 | 4 |
| 4 | 4 | 2 |
| 5 | 5 | 3 |
| 6 | 6 | 4 |
| 7 | 7 | 2 |
| 8 | 8 | 2 |
| 9 | 9 | 4 |
| 10 | 10 | 4 |
+-----------+----------+----------+
Mi intención es conseguir que la lista de third_id
s ordenados por data
campo. Ahora, ejecuté la siguiente consulta para eso.
SELECT
third_id, data
FROM
first f JOIN second s ON (s.first_id = f.first_id)
ORDER BY
data ASC;
Y obtengo el siguiente resultado como se esperaba.
+----------+------+
| third_id | data |
+----------+------+
| 4 | 5 |
| 2 | 5 |
| 4 | 5 |
| 2 | 6 |
| 3 | 6 |
| 2 | 6 |
| 2 | 7 |
| 4 | 7 |
| 4 | 7 |
| 3 | 7 |
+----------+------+
La siguiente consulta también funciona como se esperaba.
SELECT
third_id
FROM
first f JOIN second s ON (s.first_id = f.first_id)
ORDER BY
data ASC;
con la salida
+----------+
| third_id |
+----------+
| 4 |
| 2 |
| 4 |
| 2 |
| 3 |
| 2 |
| 2 |
| 4 |
| 4 |
| 3 |
+----------+
Entonces me encontré con lo siguiente.
SELECT DISTINCT
third_id
FROM
first f JOIN second s ON (s.first_id = f.first_id)
ORDER BY
data ASC;
Pero, aquí me sale un resultado inesperado:
+----------+
| third_id |
+----------+
| 2 |
| 3 |
| 4 |
+----------+
Aquí, 3
debe ser posterior 2
y 4
, ya que estoy pidiendo en el campo data
. ¿Qué estoy haciendo mal? O tengo que ir por una estrategia diferente.
Nota: Este escenario ocurre en mi proyecto. Las tablas proporcionadas aquí no pertenecen a la base de datos original. Fue creado por mí para explicar el problema. Las tablas originales contienen miles de filas. Estoy insertando volcado de base de datos si desea experimentar con los datos:
--
-- Table structure for table `first`
--
CREATE TABLE IF NOT EXISTS `first` (
`first_id` int(11) NOT NULL AUTO_INCREMENT,
`data` int(11) NOT NULL,
PRIMARY KEY (`first_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `first`
--
INSERT INTO `first` (`first_id`, `data`) VALUES
(1, 5),
(2, 6),
(3, 7),
(4, 6),
(5, 7),
(6, 5),
(7, 7),
(8, 6),
(9, 5),
(10, 7);
--
-- Table structure for table `second`
--
CREATE TABLE IF NOT EXISTS `second` (
`second_id` int(11) NOT NULL AUTO_INCREMENT,
`first_id` int(11) NOT NULL,
`third_id` int(11) NOT NULL,
PRIMARY KEY (`second_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `second`
--
INSERT INTO `second` (`second_id`, `first_id`, `third_id`) VALUES
(1, 1, 2),
(2, 2, 3),
(3, 3, 4),
(4, 4, 2),
(5, 5, 3),
(6, 6, 4),
(7, 7, 2),
(8, 8, 2),
(9, 9, 4),
(10, 10, 4);
"Pero, aquí recibo un resultado inesperado:" - Eso no es inesperado. –
@MitchWheat ¿Pero cómo? – Jomoos
Si fuera sql, rechazaría esta cláusula 'ORDER BY', pero' mysql' es notoriamente tolerante. ¿Con qué 'datos' quieres ordenar? –