獲取表中兩組不同行的計數,然後在 MySQL 中對其進行除法
為此,請使用 count(*) 並對兩個不同行集的計數求餘。讓我們首先建立一個表 -
mysql> create table DemoTable(isMarried tinyint(1)); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.17 sec)
使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
這將產生以下輸出 -
+-----------+ | isMarried | +-----------+ | 1 | | 0 | | 1 | | 1 | | 1 | +-----------+ 5 rows in set (0.00 sec)
以下是獲取表中兩組不同行計數然後對其進行分割的查詢 -
mysql> select ( select count(*) from DemoTable where isMarried=1) / ( select count(*) from DemoTable where isMarried=0 ) AS divide from dual;
這將產生以下輸出 -
+--------+ | divide | +--------+ | 4.0000 | +--------+ 1 row in set (0.00 sec)
廣告
資料結構
聯網
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP