Good morning, my question is somewhat complex. I have the following code (Very grateful McNets for the help with the formula). :
select
@cid := case when @poblacion = poblacion then @cid + 1 else @cid := 1 end as cid,
@poblacion := poblacion as p,
nombre
from
(select @cid := 1) i,
(select @poblacion := poblacion as poblacion, nombre from Clientes order by poblacion) t
Which gives me back the following:
+-----+-------------+--------+
| cid | poblacion | nombre |
+-----+-------------+--------+
| 1 | Los Angeles | John |
+-----+-------------+--------+
| 2 | Los Angeles | Pepe |
+-----+-------------+--------+
| 3 | Los Angeles | Serena |
+-----+-------------+--------+
| 1 | San Diego | Lola |
+-----+-------------+--------+
| 2 | San Diego | Juan |
+-----+-------------+--------+
| 3 | San Diego | Maria |
+-----+-------------+--------+
And in another table called Location I have the following values:
+-----+-------------+---------+
| ID | Ciudad |Iniciales|
+-----+-------------+---------+
| 1 | Los Angeles | LA |
+-----+-------------+---------+
| 2 | San Diego | SD |
+-----+-------------+---------+
| 3 | New York | NY |
+-----+-------------+---------+
I seek to concatenate the initials of the Localization table with those of the Customers table.
Staying more or less like this:
+------------+-------------+---------+
| Cod | Ciudad | Nombre |
+------------+-------------+---------+
| LA000001 | Los Angeles | John |
+-----+------+-------------+---------+
| LA000002 | Los Angeles | Pepe |
+------------+-------------+---------+
| LA000003 | Los Angeles | Serena |
+------------+-------------+---------+
| NY000001 | New York | Lola |
+------------+-------------+---------+
| NY000002 | New York | Juan |
+------------+-------------+---------+
| SD000001 | San Diego | Maria |
+------------+-------------+---------+
When making inner joins the formula does not work, with case when, if, and a million parameters I still can not merge the formula indicated above with the concatenation of the initials of the Table Location, the zeros that will disappear according to @ cid go increasing, thanks in advance!