Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在一个选择上显示2计数语法_Mysql_Sql_Group By - Fatal编程技术网

Mysql 在一个选择上显示2计数语法

Mysql 在一个选择上显示2计数语法,mysql,sql,group-by,Mysql,Sql,Group By,我有一张书单上的桌子 +---------+-------------------------+------------------+-------------+-------+----------+ | ID | Title | Writer | Publisher | year | Status | +---------+-------------------------+------------------+---

我有一张书单上的桌子

+---------+-------------------------+------------------+-------------+-------+----------+
| ID      | Title                   | Writer           | Publisher   | year  | Status   |
+---------+-------------------------+------------------+-------------+-------+----------+
| S00001  | philosophy the intro    | K. Bertens.      | Kanisius    | 2018  | BOOKED   |
| S00002  | Karl Marx: Das Kapital  | Karl Marx        | el-Classica | 2017  | READY    |
| S00003  | Karl Marx: Das Kapital  | Karl Marx        | el-Classica | 2017  | READY    |
| S00004  | Karl Marx: Das Kapital  | Johann Luwiss    | Mediatama   | 2015  | READY    |
| S00005  | Marxis                  | Karl Marx        | Photoem     | 2019  | READY    |
| S00006  | Karl Marx: Das Kapital  | Karl Marx        | el-Classica | 2017  | BOOKED   |
| S00007  | philosophy the intro    | K. Bertens.      | Kanisius    | 2018  | READY    |
+---------+-------------------------+------------------+-------------+-------+----------+
首先,我可以按标题和作者显示标题和作者组副本的计数,但我无法显示每行“就绪”状态的计数

结果应该是这样的

+-------------------------+-----------------+-------+------+
| Title                   | Writer          | Ready | stock|
+-------------------------+-----------------+-------+------+
| Marxis                  | Karl Marx       | 1     |    1 |
| Karl Marx: Das Kapital  | Johann Luwiss   | 1     |    1 |
| Karl Marx: Das Kapital  | Karl Marx       | 2     |    3 |
| philosophy the intro    | K. Bertens.     | 1     |    2 |
+-------------------------+-----------------+-------+------+

使用条件聚合:

select
    title,
    writer, 
    sum(status = 'READY') ready,
    count(*) stock
from books
group by title, writer

sum(status='READY')
是一个很好的MySQL快捷方式,可以计算满足内部条件的记录数(
status='READY'
)。它依赖于这样一个事实,即在数字上下文中,MySQL将真实条件评估为
1
(将错误条件评估为
0
)。

好吧,告诉我们你能做什么,让我们为你做所有的工作