Mysql SUBSTRING_INDEX()用于在“ - ”之后获取值

时间:2014-01-09 14:42:11

标签: mysql substring

我有以下查询

   SELECT SUBSTRING_INDEX(table1.`val1`,' ',-3) AS Value1,
   MAX(table1.`Covered Charges`) AS `Max Covered Charges`
   FROM table1

但这似乎不起作用,我有这样的结果

                   Value1                               Max Covered Charges           
    0604 - Level 1 Hospital Clinic Visits                118.320653298836
    0605 - Level 2 Hospital Clinic Visits                134.270025695931
    0013 - Level II Debridement & Destruction            146.626388077148
    0368 - Level II Pulmonary Tests                      188.440063091483
    0015 - Level III Debridement & Destruction           190.02526119403
    0607 - Level 4 Hospital Clinic Visits                220.876543209877
    0606 - Level 3 Hospital Clinic Visits                224.089367424242

我想要的是像这样的东西

                   Value1                        Max Covered Charges           
    Level 1 Hospital Clinic Visits               118.320653298836
    Level 2 Hospital Clinic Visits               134.270025695931
    Level II Debridement & Destruction           146.626388077148
    Level II Pulmonary Tests                     188.440063091483
    Level III Debridement & Destruction          190.02526119403
    Level 4 Hospital Clinic Visits               220.876543209877
    Level 3 Hospital Clinic Visits               224.089367424242

2 个答案:

答案 0 :(得分:2)

一些示例表数据可能有所帮助。

试试这个:

SELECT SUBSTRING_INDEX(table1.val1,'-',-1) AS Value1...

Reference Manual > SUBSTRING_INDEX

答案 1 :(得分:1)

试试这个

SELECT SUBSTRING_INDEX(table1.`val1`,'-',-1)AS Value1, MAX(table1.`Covered Charges`) AS `Max Covered Charges`for Selected Hospital`
FROM table1