0

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

请记住,有超过 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;
4

1 回答 1

0

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

|hotelId|cardType|etc...

我会去:

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

我在这里使用以下 SQL 对其进行了测试:

SELECT country, city, count(*)
from customers
group by country, city
ORDER BY Country;
于 2013-10-07T14:39:00.853 回答