Howsit everyone
我正在尝试做一些我认为在MYSQL上不可能的事情。
我们有一个非常大的数据库,我们从中为我们的产品提取生产报告。目前,我们运行多个查询以获取这些结果,然后手动将数据传输到表中,然后将其发送给需要报告的任何人。
是否有更简单的方法来执行此操作。 IE单个查询。
我运行的查询示例
a. SELECT count(id) from ProductA_T where created between '<date>' and '<date>' and productstatus = "<successful>";
b. SELECT count(id) from ProductB_T where created between '<date>' and '<date>' and productstatus = "<successful>";
c. SELECT count(id) from ProductC_T where created between '<date>' and '<date>' and productstatus = "<successful>";
我正在寻找
的结果示例答案 0 :(得分:1)
您可以将所有内容放入Select子句中,如下所示:
Select
(SELECT count(id) from ProductA_T where created between '<date>' and '<date>' and productstatus = "<successful>") as CountA
(SELECT count(id) from ProductB_T where created between '<date>' and '<date>' and productstatus = "<successful>") as CountB
(SELECT count(id) from ProductC_T where created between '<date>' and '<date>' and productstatus = "<successful>") as CountC
这样你就会有这样的输入:
稍后您可以轻松添加更多计数
答案 1 :(得分:0)
您可以使用UNION ALL:
SELECT 'ProductA', count(id) from ProductA_T where created between '<date>' and '<date>' and productstatus = "<successful>"
UNION ALL
SELECT 'ProductB', count(id) from ProductB_T where created between '<date>' and '<date>' and productstatus = "<successful>"
UNION ALL
SELECT 'ProductC', count(id) from ProductC_T where created between '<date>' and '<date>' and productstatus = "<successful>";
当然,您可以复制其他4种产品的选择或任何您想要的数量; - )
答案 2 :(得分:-1)
我认为您需要使用存储过程
Create StoredProcedure SP_Name_1
As
Beign
Declare @id1 int,@id2 int,@id3 int;
SELECT @id1 = count(id) from ProductA_T where created between '<date>' and '<date>' and productstatus = "<successful>";
SELECT @id2 = count(id) from ProductB_T where created between '<date>' and '<date>' and productstatus = "<successful>";
SELECT @id2 = count(id) from ProductC_T where created between '<date>' and '<date>' and productstatus = "<successful>";
select @id1 as ProductA ,@id2 as ProductB ,@id3 as ProductC
End
谢谢, 塔哈