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

เลือกค่าระหว่างสองคอลัมน์ด้วย BETWEEN ใน MySQL

ใช้ BETWEEN ตรวจสอบว่าค่าอยู่ระหว่างคอลัมน์สองคอลัมน์หรือไม่

สร้างตารางและเพิ่มข้อมูล

mysql> create table DemoTable787 ( Score1 int, Score2 int, Name varchar(100) ); 
Query OK, 0 rows affected (0.84 sec)

mysql> insert into DemoTable787 values(34,56,'Chris'); Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable787 values(73,86,'Robert'); Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable787 values(90,99,'David'); Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable787 values(80,89,'Adam'); Query OK, 1 row affected (0.21 sec)

คิวรีหาชื่อที่ 56 อยู่ระหว่าง Score1 และ Score2

mysql> select Name from DemoTable787 where 56 between Score1 and Score2;
+-------+
| Name  |
+-------+
| Chris |
+-------+
1 row in set (0.00 sec)

เงื่อนไข: 34 ≤ 56 ≤ 56 ✓ | 73 ≤ 56 ≤ 86 ✗ | 90 ≤ 56 ≤ 99 ✗ | 80 ≤ 56 ≤ 89 ✗