我使用mysqlGREATEST()函数从任一列(苹果或桃子)中选择最高整数值。

+----+------------------+
| id | apples | peaches |
+----+------------------+
|  1 |    8   |    4    |
|  2 |    2   |    6    |
|  3 |    3   |    9    |
|  4 |    7   |    2    |
|  5 |    4   |    4    |
+----+------------------+

使用$result = "SELECT GREATEST(apples, peaches) FROM table";echo $result,我得到:
8
6
9
7

在每个值旁边,我想回显相应的水果名称和一个水果图标。如何通过MYSQL查询实现下面的图像?
另外,请注意,GREATEST()函数不会显示相等的值。是否也有显示相等值的方法?

最佳答案

SELECT
IF(apples >= peaches, apples, peaches) AS quantity,
IF(apples >= peaches, 'apples', 'peaches') AS fruit
FROM ...

或者,如果您不想让apples作为equal的默认值,并且想知道两种水果的代表性是相等的:
SELECT
IF(apples >= peaches, apples, peaches) AS quantity,
CASE WHEN apples > peaches THEN 'apples' WHEN peaches > apples THEN 'apples' ELSE 'both' END AS fruit
FROM ...

关于php - 通过great()检测查询的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4356596/

10-15 16:54