将 value/field/row 列从 table 复制到另一个 table

Copy a column value/field/row from a table to another table

我有一个名为 data_table 的 table,它有一个列名 status 并且有一个列值或字段 pendingapproved 我想将那 2 个字段的计数或总和获取到另一个名为 total_status 的 table,它有一个列 total_pendingtotal_approved

实现该结果的最佳方法是什么?我正在使用 phpmyadmin 顺便查询

data_table
|专栏 1 |专栏2 |状态 |
| xxxx | xxxx |待定 |
| xxxx | xxxx |批准 |
| xxxx | xxxx |批准 |
| xxxx | xxxx |待定 |
| xxxx | xxxx |批准 |

click to see table structure

我希望结果如下所示:

total_status
| total_pending | total_approved |
| 2 | 3 |

click to see table structure

希望你明白我的意思:)

select 
  sum(case when status = 'pending' then 1 end) as total_pending,
  sum(case when status = 'approved ' then 1 end) as total_approved 
from data_table

最简单的方法是像这样使用 GROUP BY 查询:

SELECT status, count(*) as total
FROM data_table
GROUP BY status

如果你真的想要两列,你可以使用两个内联查询:

SELECT
  (SELECT count(*) FROM data_table WHERE status="Pending") AS total_pending,
  (SELECT count(*) FROM data_table WHERE status="Approved") AS total_approved

然后您可以使用以下语法创建一个新的 table:

CREATE TABLE total_status
SELECT ...your select query above...