简体   繁体   中英

Query two tables then output in one column

i want to query two tables then give the result in one column eg

table1

id   name  town 
23   john  nyc
34   mark  ATl
44   ali   Dubs


table2

cno reg
45  kln
47  dsgd
28  wer

the output i expect is

newcolumn
   23
   34
   44
   45
   47
   28

You need to use MySQL UNION .

SELECT id FROM table 1

UNION 

SELECT cno AS id FROM table2

In UNION , you can combine results from two or more database tables.

But, it needs that the selected columns should be similar.

For example, if you are fetching 5 fields from one SQL and 6 fields from another SQL.

And making UNION of these two queries, it will not work.

Above SQL works because, you are selecting one column each from SQLs.

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