使用过程构建表

时间:2013-10-07 14:31:50

标签: mysql

我正在尝试创建一个程序,该程序返回一个包含我的数据库的一些信息的表,它按照酒店每种类型的信用卡使用的客户数列出了酒店的数量

请记住,有超过50家酒店和3种类型的信用卡,我希望程序在表格中运行数据和列表

DELIMITER //    
DROP PROCEDURE IF EXISTS `testing` //
CREATE PROCEDURE `testing`(OUT param1 VARCHAR(40))

BEGIN

DECLARE id_cnpjEstabelecimento VARCHAR(40);
DECLARE id_redeCartão VARCHAR(255);

SELECT (cnpjEstabelecimento) 
FROM fpcsmovatlantica201308tst04;
SET id_cnpjEstabelecimento := cnpjEstabelecimento;

SELECT (id_redeCartão) 
FROM fpcsmovatlantica201308tst04;
SET id_redeCartão := id_redeCartão;

SELECT count(*)
FROM fpcsmovatlantica201308tst04;
WHERE redeCartão like 'id_redeCartão%';
AND cnpjEstabelecimento like 'id_cnpjEstabelecimento%';

END //
DELIMITER ;

选择

的示例
 SELECT count(*)
 FROM fpcsmovatlantica201308tst04
 WHERE redeCartão like 'Cielo%'
 AND cnpjEstabelecimento like '02223966000466%'

cnpjEstabelecimento有多个值,超过100+,因此可以进行所有选择

我甚至不必使用程序来制作它,最终的结果是

SELECT cnpjEstabelecimento, redeCartão, count(*)
FROM fpcsmovatlantica201308tst04
WHERE redeCartão like 'Cielo%'
GROUP BY cnpjEstabelecimento,redeCartão like 'Cielo%'
ORDER BY cnpjEstabelecimento ASC;

1 个答案:

答案 0 :(得分:0)

我假设你有一张桌子,看起来像这样:

|hotelId|cardType|etc...

我会选择:

Select hotelId, cardType, count(*)
from myTable
group by hotelId, cardType

我使用以下SQL测试了它here

SELECT country, city, count(*)
from customers
group by country, city
ORDER BY Country;