mysql - 从虚拟列中获取最高值

时间:2015-11-03 04:58:06

标签: mysql sql select where

SELECT *, REPLACE(number_option, 'Issue ', '') AS new_number_option FROM
jgl_article WHERE status = 1 AND topic = 'Current Issues' ORDER BY id ASC

new_number_option列返回:

1

1

1

2

2

3

3

我想从new_number_option列中获取最高值。这里3是最高值。所以,我想获得包含最高价值的记录。我在上面的查询中尝试了HAVING MAX(new_number_option)。但是,它不会起作用。

2 个答案:

答案 0 :(得分:2)

请尝试CAST,因为您的列类型为string

CAST(new_number_option as SIGNED)

使用

MAX( CAST(REPLACE(number_option, 'Issue ', '') AS UNSIGNED) )

答案 1 :(得分:1)

SELECT JA.*
  FROM
jgl_article JA INNER JOIN
     (SELECT MAX( CAST(REPLACE(number_option, 'Issue ', '') AS UNSIGNED) ) AS MAX_number_option FROM
      jgl_article) T
              ON CAST(TRIM(JA.number_option) as UNSIGNED) = T.MAX_number_option
WHERE JA.status = 1 AND JA.topic = 'Current Issues'

这将有效

希望这会有所帮助..