为此,请使用聚合函数COUNT(*)。让我们首先创建一个表-
create table DemoTable1543 -> ( -> Value1 int, -> Value2 int -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1543 values(57,60); insert into DemoTable1543 values(60,68); insert into DemoTable1543 values(90,98); insert into DemoTable1543 values(98,90);
使用select语句显示表中的所有记录-
select * from DemoTable1543;
这将产生以下输出-
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 57 | 60 | | 60 | 68 | | 90 | 98 | | 98 | 90 | +--------+--------+ 4 rows in set (0.00 sec)
这是对两列中具有相互关系的行(例如90)进行计数的查询-
select count(*) from DemoTable1543 tbl1 -> join DemoTable1543 tbl2 -> on tbl1.Value2=tbl2.Value1 -> where tbl1.Value1=90 and tbl2.Value2=90;
这将产生以下输出-
+----------+ | count(*) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec)