简体   繁体   中英

how to combine two columns in mysql and make it as one column?

+-----+---------------+---------------+
| id  |    team_A     |    team_B     |
+-----+---------------+---------------+
|  1  |   Barcelona   |  Real Madrid  |
+-----+---------------+---------------+
|  2  |     Milan     |     Inter     |
+-----+---------------+---------------+

select * from table and combine as team_c

and result should be like one column..

team_c as

barcelona
milam
real
inter

all rows should in team_c should be different.. because i need two show in an autocomplete dropdown?

试试这个查询:

select a.team_a as team_c from table a Union select b.team_b from table b

Why are you store that data into extra table. You can do using select query like

select team_a as team_c from table Union select team_b from table
SELECT team_A, team_B , CONCAT_WS('', team_A, team_B) AS team_C, from table;

You can combine them using CONCAT_WS . Hope it will usefull!

SELECT CONCAT( team_A," ",team_B) AS team_c FROM table;

You can try this output will like


Barcelona Real Madrid

Milan Inter

我认为应该可以解决你的问题:

SELECT team_A AS team_C FROM table UNION SELECT team_B FROM table

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