Join table fields with different ID

0

I have this table in MySQL:

    +-------------------------------+
    | *UDH*        |     *Text*     |
    --------------------------------
    | 050003B90301 | Hola soy A     |
    --------------------------------
    | 050003B90302 | rmin y quier   |
    --------------------------------
    | 050003B90303 | o ser amigo tuy|
    --------------------------------
    | 050003310201 | Me llamo Ana y |
    --------------------------------
    | 050003310202 | soy modelo prof|
    +-------------------------------+

As you can see, the data in the UDH column is the same as in the last character, and I need to join the Text field to one, something like this:

+-----------------------------------------------+
| *UDH* |     *Text*                            |
-------------------------------------------------
| 1     | Hola soy Armin y quiero ser amigo tuy |
-------------------------------------------------
| 2     | Me llamo Ana y soy modelo prof        |
+-----------------------------------------------+

I have dealt with the sentence: group_concat () of MySQL but I have not succeeded ... Any suggestions?

    
asked by Synapsido 21.12.2016 в 15:45
source

3 answers

1

With group concat it works, I leave you the code and an online example

SELECT substr(UDH,1,11) as id, GROUP_CONCAT( Text SEPARATOR '' ) as texto FROM test group by substr(UDH,1,11)

link

    
answered by 21.12.2016 / 16:20
source
0

Try using an Inner Join. I leave you a very interesting link. But I try to explain it to you:

link

// Consulta Mysql donde aplicamos INNER JOIN

$consulta_mysql="select NombreDelatabla1.Text,NombredeTabla2.Text
                 from NombreDelatabla1
                 inner join NombreDelaTabla2 
                 on NombreDelatabla1.UDH=NombreDelatabla2.UDH"; 
                 // <-- Aqui hacemos el inner join con la ID
    
answered by 21.12.2016 в 16:23
0

you were missing 2 things:

1.- A GROUP BY for query using only the characters you need.

2.- The most practical thing in your case is to use group_concat defining an empty separator.

First, let me clarify that the best solution would be to have in one column the unique ID and in another an incremental number since you could create indexes and optimize the queries but as I do not know how many records your database has and if it goes to have or not intensive use I only bring you the solution to your problem.

SELECT GROUP_CONCAT(text SEPARATOR '')
AS message FROM 'test_table' GROUP BY SUBSTR(udh,1,10) order by udh DESC

In this example, you should only replace test_table with the name of your table and verify the names of the fields because in my example I detail them in lowercase.

To only use the first 10 characters of UDH, the function I use to group is SUBSTR

    
answered by 21.12.2016 в 16:30