MySQL查询使用JOIN计算具有相互关系的行?
为此,请使用聚合函数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)
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志