通过maximum()检测查询字段

发布于 2024-10-06 05:39:23 字数 762 浏览 4 评论 0原文

我正在使用 mysql GREATEST() 函数从任一列(苹果或桃子)中选择最大整数值。

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

使用 $result = "SELECT GREATEST(apples,peches) FROM table";echo $result,我得到:

8
6
9
7

在每个值旁边,我想回显相应的水果名称和水果图标。如何通过MYSQL查询得到下图?

alt text

另外,请注意 GREATEST() 函数不会显示相等的值。有没有办法也显示相等的值?

I'm using the mysql GREATEST() function to select the highest integer value from either column (apples or peaches).

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

Using $result = "SELECT GREATEST(apples, peaches) FROM table"; and echo $result, I get:

8
6
9
7

Next to each value, I want to echo the corresponding fruit name and a fruit icon. How can I achieve the image below via the MYSQL query?

alt text

Also, notice that equal values aren't displayed by the GREATEST() function. Is there a way to display the equal value as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情魔剑神 2024-10-13 05:39:23
SELECT 
IF(apples >= peaches, apples, peaches) AS quantity,
IF(apples >= peaches, 'apples', 'peaches') AS fruit
FROM ...

或者,如果您不希望苹果成为平等的默认值,并且想知道两种水果何时平等地代表:

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 ...
SELECT 
IF(apples >= peaches, apples, peaches) AS quantity,
IF(apples >= peaches, 'apples', 'peaches') AS fruit
FROM ...

or, if you don't want apples to be the default on equal and want to know when both fruits are equally represented:

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 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文