2016-03-03 43 views
0

我需要加入下面的select substring的2個結果。有任何想法嗎?加入兩個Substring_index

BEGIN 
select SUBSTRING_INDEX(SUBSTRING_INDEX(oldVal, '-', n.n), '-', -1) AS oldVal5 
FROM audits oldVal 
CROSS JOIN (SELECT a.N + b.N * 10 + 1 n 
FROM (SELECT 0 AS N 
     UNION ALL SELECT 1 
     UNION ALL SELECT 2 
     UNION ALL SELECT 3 
     UNION ALL SELECT 4  
     UNION ALL SELECT 5 
     UNION ALL SELECT 6 
     UNION ALL SELECT 7 
     UNION ALL SELECT 8 
     UNION ALL SELECT 9) a , 
    (SELECT 0 AS N 
     UNION ALL SELECT 1 
     UNION ALL SELECT 2 
     UNION ALL SELECT 3 
     UNION ALL SELECT 4 
     UNION ALL SELECT 5 
     UNION ALL SELECT 6 
     UNION ALL SELECT 7 
     UNION ALL SELECT 8 
     UNION ALL SELECT 9) b ORDER BY n) n 
WHERE n.n <= 1 + (LENGTH(oldVal) - LENGTH(REPLACE(oldVal, '-', ''))) and 
dbUser=34 and tableName='persona' and task='u'; 

select SUBSTRING_INDEX(SUBSTRING_INDEX(newVal, '-', n.n), '-', -1) AS newval5 
FROM audits newVal 
CROSS JOIN (SELECT a.N + b.N * 10 + 1 n 
FROM (SELECT 0 AS N 
UNION ALL SELECT 1 
UNION ALL SELECT 2 
UNION ALL SELECT 3 
UNION ALL SELECT 4 
UNION ALL SELECT 5 
UNION ALL SELECT 6 
UNION ALL SELECT 7 
UNION ALL SELECT 8 
UNION ALL SELECT 9) a , 
(SELECT 0 AS N 
UNION ALL SELECT 1 
UNION ALL SELECT 2 
UNION ALL SELECT 3 
UNION ALL SELECT 4 
UNION ALL SELECT 5 
UNION ALL SELECT 6 
UNION ALL SELECT 7 
UNION ALL SELECT 8 
UNION ALL SELECT 9) b ORDER BY n) n 
WHERE n.n <= 1 + (LENGTH(newVal) - LENGTH(REPLACE(newVal, '-', ''))) and 
dbUser=34 and tableName='persona' and task='u'; 

END 

回答

0

只需在同一查詢中選擇兩者。

select SUBSTRING_INDEX(SUBSTRING_INDEX(oldVal, '-', n.n), '-', -1) AS oldVal5, 
     SUBSTRING_INDEX(SUBSTRING_INDEX(newVal, '-', n.n), '-', -1) AS newVal5 
FROM audits oldVal 
CROSS JOIN (SELECT a.N + b.N * 10 + 1 n 
FROM (SELECT 0 AS N 
     UNION ALL SELECT 1 
     UNION ALL SELECT 2 
     UNION ALL SELECT 3 
     UNION ALL SELECT 4  
     UNION ALL SELECT 5 
     UNION ALL SELECT 6 
     UNION ALL SELECT 7 
     UNION ALL SELECT 8 
     UNION ALL SELECT 9) a , 
    (SELECT 0 AS N 
     UNION ALL SELECT 1 
     UNION ALL SELECT 2 
     UNION ALL SELECT 3 
     UNION ALL SELECT 4 
     UNION ALL SELECT 5 
     UNION ALL SELECT 6 
     UNION ALL SELECT 7 
     UNION ALL SELECT 8 
     UNION ALL SELECT 9) b ORDER BY n) n 
WHERE (n.n <= 1 + (LENGTH(oldVal) - LENGTH(REPLACE(oldVal, '-', ''))) 
     or 
     n.n <= 1 + (LENGTH(newVal) - LENGTH(REPLACE(newVal, '-', '')))) 
    and dbUser=34 and tableName='persona' and task='u';