PHP instruction for a select group by having

0

I need to do a comparison of values and retrieve the data between two tables.

It's an old-fashioned bingo system, only the cards will be online, the draw will be at the base of the globe, by balls called.

System:

  • The user enters the website and, after login, prepares his / her virtual card by choosing 18 tens of 60 (between 01 and 60).

Problem:

  • The drawn ball will have a value between 01 and 60, the card has 18 random tips chosen by the user.

  • I need to find the merge and retrieve the data:

Data Model:

CREATE TABLE IF NOT EXISTS 'cartela' (
  'numero_cartela' int(11) NOT NULL AUTO_INCREMENT,
  'nro_da_sorte' int(11) NOT NULL,
  'user_id' int(11) NOT NULL,
  'nome' varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  'numero_whats' varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  'id_indicador' int(11) NOT NULL,
  'whats_indicador' varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  'id_base' int(11) NOT NULL,
  'nome_indicador' varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  'numero_sorteio' int(11) NOT NULL,
  PRIMARY KEY ('numero_cartela'),
  KEY 'fk_cartela_sorteio1_idx' ('numero_sorteio')
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

INSERT INTO 'cartela' ('numero_cartela', 'nro_da_sorte', 'user_id', 'nome', 'numero_whats', 'id_indicador', 'whats_indicador', 'id_base', 'nome_indicador', 'numero_sorteio') VALUES
(2, 123456, 7, 'andsoave', '41984009859', 7, '41984009859', 62, 'anderson', 1);

ALTER TABLE 'cartela'
  ADD CONSTRAINT 'fk_cartela_sorteio1' FOREIGN KEY ('numero_sorteio') REFERENCES 'sorteio' ('numero_sorteio') ON DELETE NO ACTION ON UPDATE NO ACTION;


CREATE TABLE IF NOT EXISTS 'cartela_bola' (
  'idcartela_bola' int(11) NOT NULL AUTO_INCREMENT,
  'numero_bola' int(11) DEFAULT NULL,
  'numero_cartela' int(11) NOT NULL,
  'numero_sorteio' int(11) NOT NULL,
  PRIMARY KEY ('idcartela_bola'),
  KEY 'fk_cartela_bola_cartela_idx' ('numero_cartela'),
  KEY 'fk_cartela_bola_sorteio1_idx' ('numero_sorteio')
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

INSERT INTO 'cartela_bola' ('idcartela_bola', 'numero_bola', 'numero_cartela', 'numero_sorteio') VALUES
(2, 1, 2, 1),
(3, 2, 2, 1),
(4, 3, 2, 1);

ALTER TABLE 'cartela_bola'
  ADD CONSTRAINT 'fk_cartela_bola_cartela' FOREIGN KEY ('numero_cartela') REFERENCES 'cartela' ('numero_cartela') ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT 'fk_cartela_bola_sorteio1' FOREIGN KEY ('numero_sorteio') REFERENCES 'sorteio' ('numero_sorteio') ON DELETE NO ACTION ON UPDATE NO ACTION;


CREATE TABLE IF NOT EXISTS 'sorteio' (
  'numero_sorteio' int(11) NOT NULL AUTO_INCREMENT,
  'data' varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  'hora' varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY ('numero_sorteio')
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;

-

INSERT INTO 'sorteio' ('numero_sorteio', 'data', 'hora') VALUES
(1, '15/09/2018', '10:00');


CREATE TABLE IF NOT EXISTS 'sorteio_bola' (
  'idsorteio_bola' int(11) NOT NULL AUTO_INCREMENT,
  'nomero_bola' int(11) NOT NULL,
  'numero_sorteio' int(11) NOT NULL,
  PRIMARY KEY ('idsorteio_bola'),
  KEY 'fk_sorteio_bola_sorteio1_idx' ('numero_sorteio')
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;

INSERT INTO 'sorteio_bola' ('idsorteio_bola', 'nomero_bola', 'numero_sorteio') VALUES
(4, 1, 1),
(5, 2, 1),
(6, 3, 1);

ALTER TABLE 'sorteio_bola'
  ADD CONSTRAINT 'fk_sorteio_bola_sorteio1' FOREIGN KEY ('numero_sorteio') REFERENCES 'sorteio' ('numero_sorteio') ON DELETE NO ACTION ON UPDATE NO ACTION;

I left the INSERTS to serve as an example only.

SQL:

select c.numero_sorteio,c.numero_cartela,count(c.nomero_bola)
from   cartela_bola c,sorteio_bola select
where  c.numero_sorteio = s.numero_sorteio
and    c.nomero_bola = s.nomero_bola
group by c.numero_sorteio,c.numero_cartela
having count(c.nomero_bola) >= 16

My difficulty:

Given these data, I have to search for the values of the correct cards, to save in another table and as I said earlier, I'm not sure how to execute this statement in PHP and MySQL.

I needed an example to follow, can anyone give me a hint?

    
asked by anonymous 27.08.2018 / 23:43

0 answers