加入列并排序

时间:2014-06-04 09:42:40

标签: mysql

我无法做到,这是我想要建立的真实情况

SELECT vertical.ext, vertical.pengguna, vertical.nostaf, vertical.pengguna2, vertical.nostaf2, vertical.pengguna3, vertical.nostaf3, cil_personal.*
FROM cil_personal
JOIN vertical ON (vertical.ext = cil_personal.ext)
WHERE cil_personal.year = 2014 AND cil_personal.month = 06  
ORDER BY nostaf,nostaf2,nostaf3 ASC

我得到了这样的结果

column column column column column column  nostaf  nostaf2 nostaf3
  txt    txt    txt    txt    txt    txt     10 
  txt    txt    txt    txt    txt    txt     11  
  txt    txt    txt    txt    txt    txt             213
  txt    txt    txt    txt    txt    txt                     45
  txt    txt    txt    txt    txt    txt     30   
  txt    txt    txt    txt    txt    txt     55 

it should be

column column column column column column  nostaf  nostaf2 nostaf3
  txt    txt    txt    txt    txt    txt     10 
  txt    txt    txt    txt    txt    txt     11  
  txt    txt    txt    txt    txt    txt     30        
  txt    txt    txt    txt    txt    txt             45          
  txt    txt    txt    txt    txt    txt     55        
  txt    txt    txt    txt    txt    txt                      213



or

column column column column column column allstaf
  txt    txt    txt    txt    txt    txt     10 
  txt    txt    txt    txt    txt    txt     11  
  txt    txt    txt    txt    txt    txt     30       
  txt    txt    txt    txt    txt    txt     45              
  txt    txt    txt    txt    txt    txt     55   
  txt    txt    txt    txt    txt    txt     213  

2 个答案:

答案 0 :(得分:0)

SELECT T1.* FROM table1 T1 LEFT JOIN table2 AS T2 ON T1.id1 = T2.allid GROUP BY T2.allid;

根据您的使用情况,您可以使用不同类型的联接。

答案 1 :(得分:0)

不确定表的数据结构,但下面的查询应该这样做。但是,您可能会得到一个空白行,以消除您需要在查询中添加条件 where allname <> ''如果您为该名称字段添加一些'',或者如果它的NULL然后使用 where allname is not null您还可以将union更改为union all,这样会更好。

select allname,allid
from
( 
  select name1 as allname, id1 as allid from your_table
  union
  select name2 as allname, id2 as allid from your_table
  union 
  select name3 as allname, id3 as allid from your_table
)x
order by allid