临时表具有每个其他表的计数

时间:2012-06-22 08:25:44

标签: mysql sql pdo mysqli

我正在尝试在我的php脚本中创建一个统计页面。为了从每个表中选择计数,我需要超过30个这样的查询

SELECT COUNT(order_id) as `uncompleted_orders` FROM `orders` WHERE `order_status` != 0

然后我需要运行这样的另一个查询:

SELECT COUNT(order_id) as `completed_orders` FROM `orders` WHERE `order_status` = 1

我尝试过这种方法,但它不起作用:

SELECT COUNT(order_id) as `uncompleted_orders` FROM `sd_orders` WHERE `order_status` != 4;
SELECT COUNT(order_id) as `completed_orders` FROM `sd_orders` WHERE `order_status` = 4;

有没有办法在MySQL中创建一个新的临时表包含其他表的计数?

2 个答案:

答案 0 :(得分:4)

您可以尝试这样的事情:

SELECT 
(
   SELECT COUNT(order_id) FROM `sd_orders` WHERE `order_status` != 4
)  as `uncompleted_orders`,
(
   SELECT COUNT(order_id) FROM `sd_orders` WHERE `order_status` = 4
)  as `completed_orders`

每个计数都有一行和一个字段的结果集。

答案 1 :(得分:0)

如果没有更多信息,就无法概括,但有很多结构可以帮助你。


首先,您的示例实际上来自一个表,而不是两个。这意味着您可以执行以下操作...

SELECT
  COUNT(CASE WHEN order_status =  4 THEN order_id END) AS   complete_orders,
  COUNT(CASE WHEN order_status <> 4 THEN order_id END) AS incomplete_orders
FROM
  sd_orders

这是有效的,因为COUNT(<something>)在结果中不包含NULL。并且通过不包括ELSE子句,任何不匹配的内容都返回NULL。人们完成相同结果的另一种方式是SUM(CASE WHEN ? THEN 1 ELSE 0 END)


其次,在你实际拥有多个表的地方,你可以用几种不同的方式组合结果......

-- Where you want one value from each table...
--------------------------------------------------------------------------------
SELECT
  (SELECT COUNT(*) FROM table1 WHERE fieldx = ?)   AS value1,
  (SELECT COUNT(*) FROM table2 WHERE fieldy = ?)   AS value2


-- Where you want one row of values from each table...
--------------------------------------------------------------------------------
SELECT
  table1_summary.value1 AS table1_value1,
  table1_summary.value2 AS table1_value2,
  table2_summary.value1 AS table2_value1,
  table2_summary.value2 AS table2_value2
FROM
(
  SELECT
    COUNT(CASE WHEN fieldx  = ? THEN id END) AS value1,
    COUNT(CASE WHEN fieldx <> ? THEN id END) AS value2
  FROM
    table1
)
  AS table1_summary
CROSS JOIN
(
  SELECT
    COUNT(CASE WHEN fieldy  = ? THEN id END) AS value1,
    COUNT(CASE WHEN fieldy <> ? THEN id END) AS value2
  FROM
    table2
)
  AS table2_summary


-- Where you want many rows, but of the same fields, from each table...
--------------------------------------------------------------------------------
SELECT
  *
FROM
(
  SELECT
    'Table1'                                 AS source_table,
    fielda                                   AS some_grouping,
    COUNT(CASE WHEN fieldx  = ? THEN id END) AS value1,
    COUNT(CASE WHEN fieldx <> ? THEN id END) AS value2
  FROM
    table1
  GROUP BY
    fielda

  UNION ALL

  SELECT
    'Table2'                                 AS source_table,
    fieldb                                   AS some_grouping,
    COUNT(CASE WHEN fieldy  = ? THEN id END) AS value1,
    COUNT(CASE WHEN fieldy <> ? THEN id END) AS value2
  FROM
    table2
  GROUP BY
    fieldb
)
  AS summary
ORDER BY
  source_table,
  some_grouping,
  value1,
  value2


正如您所看到的,有很多方法可以做到这一点。 如何接近它完全取决于你的数据和你的需求。