2011-08-05 45 views
3

我做这个选择:的MySQL获得唯一键的同时选择

select code,name from talbe1 
union all 
select code,name from table2 

的实际代码心不是对我很重要,但什么是对我来说重要的是,代码列将是唯一的列,以及与此选择我不能承授人吧.. 是否有保存Word /东西会给我这样的事情:

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

感谢。

回答

1

看一看的mysql UUID call。这会导致类似这样的事情:

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

删除 “所有”:

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

Union all保持人行。
Union删除重复项。

如果您对每个表中的相同的代码不同的名称,你必须选择一个名字 - 试试这个:

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