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

mysqlmysqli database

让我们首先创建一个表,其中有一列作为 ID −

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

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

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)

使用 select 语句显示表中的所有记录 −

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)

相关文章