Nir
Nir

Reputation: 2649

MySQL get unique key while selecting

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.

Upvotes: 3

Views: 670

Answers (2)

joostschouten
joostschouten

Reputation: 3893

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)

Upvotes: 1

Bohemian
Bohemian

Reputation: 425288

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

Upvotes: 0

Related Questions