Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

หาค่าสูงสุดในแถว (Row-wise MAX) ใน MySQL ด้วย GREATEST()

ใช้ GREATEST() เพื่อหาค่ามากที่สุดจากหลายคอลัมน์ในแถวเดียวกัน (ต่างจาก MAX() ที่หาในคอลัมน์)

ตัวอย่าง

mysql> create table DemoTable ( Value1 int, Value2 int, Value3 int );
Query OK, 0 rows affected (0.58 sec)
mysql> insert into DemoTable values(190,395,322); Query OK, 1 row affected (0.16 sec)

ข้อมูล

mysql> select * from DemoTable;
+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
| 190    | 395    | 322    |
+--------+--------+--------+

ใช้ GREATEST()

mysql> select greatest(Value1,Value2,Value3) as GreaterValue from DemoTable;
+--------------+
| GreaterValue |
+--------------+
| 395          |
+--------------+

ฟังก์ชัน LEAST() ใช้หาค่าน้อยที่สุดในลักษณะเดียวกัน