MySql表按Split字符串排序

时间:2011-12-24 08:32:26

标签: mysql sql-order-by

指导解决方案是否可行。

enter image description here

我需要表格结果如下

 RAJ1  RAJ2  ARUN  MUTHU
 -----------------------
 86     74    45   null
 74     86    45   null
 null   74    45    86

我不知道从哪里开始通过程序或普通查询来实现它。

1 个答案:

答案 0 :(得分:1)

SELECT
  COALESCE(
    IF(LEFT(subj1,4)='RAJ1', SUBSTRING(subj1,6), NULL),
    IF(LEFT(subj2,4)='RAJ1', SUBSTRING(subj2,6), NULL),
    IF(LEFT(subj3,4)='RAJ1', SUBSTRING(subj3,6), NULL)) as RAJ1,
  COALESCE(
    IF(LEFT(subj1,4)='RAJ2', SUBSTRING(subj1,6), NULL),
    IF(LEFT(subj2,4)='RAJ2', SUBSTRING(subj2,6), NULL),
    IF(LEFT(subj3,4)='RAJ2', SUBSTRING(subj3,6), NULL)) as RAJ2,
  COALESCE(
    IF(LEFT(subj1,4)='ARUN', SUBSTRING(subj1,6), NULL),
    IF(LEFT(subj2,4)='ARUN', SUBSTRING(subj2,6), NULL),
    IF(LEFT(subj3,4)='ARUN', SUBSTRING(subj3,6), NULL)) as ARUN,
  COALESCE(
    IF(LEFT(subj1,5)='MUTHU', SUBSTRING(subj1,7), NULL),
    IF(LEFT(subj2,5)='MUTHU', SUBSTRING(subj2,7), NULL),
    IF(LEFT(subj3,5)='MUTHU', SUBSTRING(subj3,7), NULL)) as MUTHU
FROM thetable;