So, I've got this 3 table relationship where "lampara_generica" gets substituted by "lamapara_nitide"
Now, I only want the better substitute, the one which have the lower value in "lampara_nitide" for the column "w_real".
Im doing the next query and the resultset is the expected:
SELECT lampara_nitide_has_lampara_generica.* , lampara_nitide.*
FROM lampara_nitide_has_lampara_generica
INNER JOIN lampara_nitide on lampara_nitide.id = lampara_nitide_has_lampara_generica.lampara_nitide_id
+-------------------+---------------------+----+--------+-----------+-----------+---------------------+---------------------+
| lampara_nitide_id | lampara_generica_id | id | nombre | w_teorico | w_real | horas_mantenimiento | coste_mantenimiento |
+-------------------+---------------------+----+--------+-----------+-----------+---------------------+---------------------+
| 1 | 1 | 1 | 4 | 4.0000000 | 0.5000000 | 4 | 4 |
| 2 | 1 | 2 | 5 | 5.0000000 | 0.2500000 | 5 | 5 |
| 2 | 2 | 2 | 5 | 5.0000000 | 0.2500000 | 5 | 5 |
| 3 | 1 | 3 | 6 | 6.0000000 | 0.1000000 | 6 | 6 |
| 3 | 3 | 3 | 6 | 6.0000000 | 0.1000000 | 6 | 6 |
+-------------------+---------------------+----+--------+-----------+-----------+---------------------+---------------------+
BUT once I edit it to get the MIN
for the "w_real" col and grouping by
"lampara_generica_id" all the id cols get messed.
among other things I've tried this:
SELECT lampara_nitide_has_lampara_generica.* , lampara_nitide.* , min(w_real)
FROM lampara_nitide_has_lampara_generica
INNER JOIN lampara_nitide on lampara_nitide.id = lampara_nitide_has_lampara_generica.lampara_nitide_id
group by lampara_generica_id
+-------------------+---------------------+----+--------+-----------+-----------+---------------------+---------------------+-------------+
| lampara_nitide_id | lampara_generica_id | id | nombre | w_teorico | w_real | horas_mantenimiento | coste_mantenimiento | min(w_real) |
+-------------------+---------------------+----+--------+-----------+-----------+---------------------+---------------------+-------------+
| 1 | 1 | 1 | 4 | 4.0000000 | 0.5000000 | 4 | 4 | 0.1000000 |
| 2 | 2 | 2 | 5 | 5.0000000 | 0.2500000 | 5 | 5 | 0.2500000 |
| 3 | 3 | 3 | 6 | 6.0000000 | 0.1000000 | 6 | 6 | 0.1000000 |
+-------------------+---------------------+----+--------+-----------+-----------+---------------------+---------------------+-------------+
Anybody has a clue on how this query shall be done?
Interesting link -> here