开发者

MySQL get unique key while selecting

开发者 https://www.devze.com 2023-03-25 08:11 出处:网络
I\'m making this select: select code,name from开发者_开发技巧 talbe1 union all select code,name from table2

I'm making this select:

select code,name from开发者_开发技巧 talbe1
union all
select code,name from table2

The actual code isnt important to me but what is important to me is that the code column will be unique column, and with this select I cant grantee it.. Is there any save word/something that will give me something like that:

select getUniqueCode(),name from(
select name from talbe1
union all
select name from table2)

Thanks.


Have a look at the mysql UUID call. Which would result in something like this:

select UUID(),name from(
select name from talbe1
union all
select name from table2)


remove "all":

select code,name from table1
union
select code,name from table2

Union all keeps al rows.
Union removes duplicates.

If you have different names for the same code in each table, you have to pick one name - try this:

select code, max(name) as name
from (select code,name from table1
    union
    select code,name from table2) x
group by 1
0

精彩评论

暂无评论...
验证码 换一张
取 消