简体   繁体   中英

Joining three tables based on only one key

Hi Good day everyone,

table_1

id       c2
-----------
id4     5     
id1     5
id3      4
id2     4

table_2

-----------
id       c1
-----------
id1      5     
id2      5
id3      4
id4      4 

table_3

-----------
id       c1
-----------
id3      5     
id1      5
id2     4
id4      4

After joining these three tables , I would to get as the following table. target

----------------
id       c1c2c3
----------------
id1      555     
id2      544
id4      454
id3      445

If you don't mind help me , please.But my tables have only one unique key.My database is mysql 5.1.

Try this

SELECT T1.ID,concat(T2.c1,T1.c2,T3.c3 ) AS C1C2C3
FROM Table1 T1 JOIN Table2 T2 On T1.id =T2.id JOIN Table3 T3 On T3.id =T1.id
ORDER BY RIGHT(T1.ID,1)

Fiddle Demo


O/P:


id       c1c2c3
----------------
id1      555     
id2      544
id3      445
id4      454
select t1.id, concat(t2.c1,t1.c2,t3.c3 ) as 'c1c2c3' from table_1 t1 inner
join table_2 t2 inner join table_3 t3 on t2.id=t1.id 
and t3.id=t1.id and t3.id=t2.id order by c1c2c3 desc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM