错误在" ORDER BY FIELD"

时间:2017-07-11 09:22:31

标签: mysql

我在MYSQL中写了一个查询:

SELECT DISTINCT MeasurementRowIndex, X, Y, Z 
FROM GeneralResultsTable 
WHERE MainDataIndex IN (1, 2) 
ORDER BY FIELD(MeasurementRowIndex , 2, 4, 3, 1, 7, 8, 6, 5)

我得到了:

  

' FIELD'无法识别内置函数名称。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

如果您的MySQL版本中没有FIELD(),那么您必须解释MeasurementRowIndex并将其转换为提升的数字:

SELECT DISTINCT MeasurementRowIndex, X, Y, Z FROM GeneralResultsTable WHERE MainDataIndex IN (1, 2) 
ORDER BY CASE MeasurementRowIndex 
  WHEN 2 THEN 1 
  WHEN 4 THEN 2 
  WHEN 3 THEN 3 
  WHEN 1 THEN 4 
  WHEN 7 THEN 5 
  WHEN 8 THEN 6 
  WHEN 6 THEN 7 
  WHEN 5 THEN 8 
END

答案 1 :(得分:0)

SELECT DISTINCT MeasurementRowIndex,X,Y,Z,FIELD(MeasurementRowIndex,2, 4,3,1,7,8,6,5) 来自GeneralResultsTable WHERE MainDataIndex IN(1,2) 按字段排序(MeasurementRowIndex,2,4,3,1,7,8,6,5)

答案 2 :(得分:-1)

SELECT DISTINCT MeasurementRowIndex, X, Y, Z, FIELD(MeasurementRowIndex , 2, 
4, 3, 1, 7, 8, 6, 5) 
FROM GeneralResultsTable 
WHERE MainDataIndex IN (1, 2) 
ORDER BY FIELD(MeasurementRowIndex , 2, 4, 3, 1, 7, 8, 6, 5)