0

我有一个名为“tblVersion”的表,看起来像......

| key           |  value  |
|-------------------------|
| buildVersion  |  5      |
| minorVersion  |  4      |
| majorVersion  |  2      |

我想构建一个返回字符串“2.4.5”的查询,即majorVersion.minorVersion.buildVersion。

到目前为止我有

SELECT GROUP_CONCAT(tblVersion.value SEPARATOR '.' ) AS softwareVersion 
FROM tblVersion 
WHERE tblVersion.key = 'majorVersion'
OR tblVersion.key = 'minorVersion' OR tblVersion.key = 'buildVersion'

这将返回“5.2.4”,我似乎无法以正确的顺序获取字符串。

是否可以具体说明值的显示顺序?

4

3 回答 3

2

使用顺序FIELD

SELECT GROUP_CONCAT(value order by  FIELD(tblVersion.key , 'majorVersion', 'minorVersion' , 'buildVersion') SEPARATOR '.' ) AS softwareVersion 
FROM tblVersion
WHERE tblVersion.key = 'majorVersion'
OR tblVersion.key = 'minorVersion' OR tblVersion.key = 'buildVersion';

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field

演示:http ://sqlfiddle.com/#!2/8ef367/4

于 2013-05-08T10:50:35.863 回答
2

您应该能够在函数中使用CASE表达式:GROUP_CONCAT

select
  group_concat(`value` 
               order by 
                 case `key` 
                   when 'majorVersion' then 0 
                   when 'minorVersion' then 1
                   else 'buildVersion' end SEPARATOR '.') SoftwareVersion
from tblVersion

请参阅带有演示的 SQL Fiddle

于 2013-05-08T12:10:57.417 回答
0

如果 DhruvPathak 在您实际追求的方面是正确的,那么可以通过这种方式实现......

SELECT GROUP_CONCAT(x.value ORDER BY FIELD(x.key,'minorversion','majorversion','buildversion') DESC SEPARATOR '.') softwareVersion 
  FROM tblversion x 
 WHERE x.key IN('minorVersion','majorVersion','buildVersion');
于 2013-05-08T11:53:16.613 回答