查找 ID 可被 4 整除的所有行的 MySQL 查询?


让我们先使用其中一列作为 ID 创建一个表 -

mysql> create table DemoTable
   (
   ID int,
   StudentName varchar(10),
   CountryName varchar(20)
   );
Query OK, 0 rows affected (0.70 sec)

使用插入命令在表中插入一些记录 -

mysql> insert into DemoTable values(0,'David','AUS');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(3,'Chris','UK');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values(8,'Carol','US');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values(9,'Sam','US');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(12,'Robert','UK');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values(10,'Mike','AUS');
Query OK, 1 row affected (0.17 sec)

使用选择语句从表中显示所有记录 -

mysql> select *from DemoTable;

输出

+------+-------------+-------------+
| ID   | StudentName | CountryName |
+------+-------------+-------------+
| 0    | David       | AUS         |
| 3    | Chris       | UK          |
| 8    | Carol       | US          |
| 9    | Sam         | US          |
| 12   | Robert      | UK          |
| 10   | Mike        | AUS         |
+------+-------------+-------------+
6 rows in set (0.00 sec)

以下是查找 ID 可被 4 整除的所有行的查询 -

mysql> select *from DemoTable where (ID%4)=0;

输出

+------+-------------+-------------+
| ID   | StudentName | CountryName |
+------+-------------+-------------+
| 0    | David       | AUS         |
| 8    | Carol       | US          |
| 12   | Robert      | UK          |
+------+-------------+-------------+
3 rows in set (0.30 sec)

更新于:2019-07-30

447 次查看

启动你的 职业

完成课程获得认证

开始学习
广告