Search code examples
mysqlleft-joininner-joinouter-join

Query: Count how many times a parameter in one table does not exist in another


As a title, I have two tables one of the products with relative id, and one of the events that through an inner join after the registration sees involved in the various events different id of products. At an event I can participate in more products.

The table of products is called: store_locator

instead the table of events is called: campagne_cliente

CREATE TABLE `campagne_cliente` (
  `id_campagna_cliente` int(11) NOT NULL,
  `cliente_id_campagna` int(11) NOT NULL,
  `impianto_id_campagna` int(11) NOT NULL,
  `nome_campagna_cliente` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

where impianto_id_campagna are the ids of the store_locator table (products)

This is my goal. When registering an event, the user will select a customer for more products.

Once finished this process of course I will have in the events table

the id of the event
the customer's id
the product id
name of the event: optional
(see Table: campagne_cliente)

practical case:

  `id_campagna_cliente` = 12
  `cliente_id_campagna` = 34
  `impianto_id_campagna` = 6
  `nome_campagna_cliente` = optional name

obviously the id number 6 contained in the table campagne_cliente, refers to the id number 6 of the store_locator table

Here my question: If a user accidentally deletes the number 6 from the store_locator table, obviously when I go to see the info page of that event I will not see the product details number 6

How can I count how many times from the table campagne_cliente the product id does not exist in the store_locator table

my try:

SELECT COUNT(*)
FROM campagne_cliente
RIGHT OUTER JOIN  store_locator
ON  campagne_cliente.impianto_id_campagna = store_locator.id
WHERE store_locator.id = 0 

but no good.

@Alexey

in my project in addition to these two tables there is also another table that should be involved, The table I'm talking about is called: campagne

CREATE TABLE `campagne` (
  `id_campagna` int(11) NOT NULL,
  `data_inizio` date NOT NULL,
  `data_fine` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

This table has a parameter called:id_campagna Because this table exists: because it allows the registration of the campaign (event) by setting a start and end date, which then in the next step where the user associates all products to the customer, these data end up in the table:campagne_cliente

I also put the structure of the store_locator table in it

CREATE TABLE `store_locator` (
  `id` bigint(20) NOT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `post_id` bigint(20) DEFAULT NULL,
  `category_id` varchar(11) DEFAULT NULL,
  `name` varchar(160) DEFAULT NULL,
  `logo` varchar(255) DEFAULT NULL,
  `address` varchar(160) DEFAULT NULL,
  `lat` varchar(20) DEFAULT NULL,
  `lng` varchar(20) DEFAULT NULL,
  `url` varchar(160) DEFAULT NULL,
  `description` text,
  `tel` varchar(30) DEFAULT NULL,
  `email` varchar(60) DEFAULT NULL,
  `city` varchar(60) DEFAULT NULL,
  `cap` varchar(255) NOT NULL,
  `country` varchar(60) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `concessionaria` varchar(255) NOT NULL,
  `proprietaria` varchar(255) NOT NULL,
  `cimasa` varchar(255) NOT NULL,
  `inpe` varchar(255) NOT NULL,
  `tipo_impianto` varchar(255) NOT NULL,
  `tipologia` varchar(255) NOT NULL,
  `numero_facce` varchar(255) NOT NULL,
  `quartiere` varchar(255) NOT NULL,
  `comune` varchar(255) NOT NULL,
  `ubicazione` varchar(255) NOT NULL,
  `circuito` varchar(255) NOT NULL,
  `costo_quattordici` varchar(255) NOT NULL,
  `costo_mese` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

and a practiche example, this are the value content in the table store_locator, you can see, there isn't id 44:

INSERT INTO `store_locator` (`id`, `user_id`, `post_id`, `category_id`, `name`, `logo`, `address`, `lat`, `lng`, `url`, `description`, `tel`, `email`, `city`, `cap`, `country`, `created`, `concessionaria`, `proprietaria`, `cimasa`, `inpe`, `tipo_impianto`, `tipologia`, `numero_facce`, `quartiere`, `comune`, `ubicazione`, `circuito`, `costo_quattordici`, `costo_mese`) VALUES
(1, NULL, NULL, '3x2', NULL, NULL, 'Viale XVII Olimpiade', '41.932294', '12.472014', NULL, NULL, NULL, NULL, 'Roma', '00196', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'AZ830', 'Tradizionale', 'Tradizionale', 'Poster', 'bifacciale', '', 'Roma', '', '', ' € 10.000 ', ' € 20.000 '),
(2, NULL, NULL, '3x2', NULL, NULL, 'Via dei Campi Sportivi, 43', '41.937209', '12.491009', NULL, NULL, NULL, NULL, 'Roma', '00197', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'AZ834', 'Tradizionale', 'Tradizionale', 'Poster', 'monofacciale', 'Monte Antenne', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(3, NULL, NULL, '3x2', NULL, NULL, 'Viale Maresciallo Pilsudski', '41.927768', '12.475042', NULL, NULL, NULL, NULL, 'Roma', '00196', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'AZ837', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(7, NULL, NULL, '3x2', NULL, NULL, 'Viale XVII Olimpiade', '41.932294', '12.472014', NULL, NULL, NULL, NULL, 'Roma', '00196', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'AZ830', 'Tradizionale', 'Tradizionale', 'Poster', 'bifacciale', '', 'Roma', '', '', ' € 10.000 ', ' € 20.000 '),
(8, NULL, NULL, '3x2', NULL, NULL, 'Via dei Campi Sportivi, 43', '41.937209', '12.491009', NULL, NULL, NULL, NULL, 'Roma', '00197', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'AZ834', 'Tradizionale', 'Tradizionale', 'Poster', 'monofacciale', 'Monte Antenne', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(9, NULL, NULL, '3x2', NULL, NULL, 'Viale Maresciallo Pilsudski', '41.927768', '12.475042', NULL, NULL, NULL, NULL, 'Roma', '00196', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'AZ837', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(10, NULL, NULL, '3x2', NULL, NULL, 'Viale di Val Fiorita, 25', '41.839818', '12.464565', NULL, NULL, NULL, NULL, 'Roma', '00144', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB106', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(11, NULL, NULL, '3x2', NULL, NULL, 'Via Salaria, 1075', '41.977152', '12.505168', NULL, NULL, NULL, NULL, 'Roma', '00138', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB608', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(12, NULL, NULL, '3x2', NULL, NULL, 'Piazzale Aldo Moro', '41.902230', '12.511968', NULL, NULL, NULL, NULL, 'Roma', '00185', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB616', '', 'Tradizionale', 'Poster', 'bifacciale', '', 'Roma', '', '', ' € 10.000 ', ' € 20.000 '),
(13, NULL, NULL, '3x2', NULL, NULL, 'Via Nomentana, 696', '41.937860', '12.540871', NULL, NULL, NULL, NULL, 'Roma', '00141', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB660', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(14, NULL, NULL, '3x2', NULL, NULL, 'Via Nomentana, 771', '41.937887', '12.544061', NULL, NULL, NULL, NULL, 'Roma', '00137', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB676', '', 'Tradizionale', 'Poster', '', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(15, NULL, NULL, '3x2', NULL, NULL, 'Piazza Ottaviano Vimercati, 37', '41.958539', '12.524797', NULL, NULL, NULL, NULL, 'Roma', '0139', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB708', '', 'Tradizionale', 'Poster', 'bifacciale', '', 'Roma', '', '', ' € 10.000 ', ' € 20.000 '),
(16, NULL, NULL, '3x2', NULL, NULL, 'Via Cavriglia, 10', '41.947661', '12.516421', NULL, NULL, NULL, NULL, 'Roma', '00139', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB708', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(17, NULL, NULL, '3x2', NULL, NULL, 'Via Monte Rocchetta, 28', '41.947396', '12.531392', NULL, NULL, NULL, NULL, 'Roma', '00139', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB730', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(18, NULL, NULL, '3x2', NULL, NULL, 'Via Monte Cervialto, 233', '41.956934', '12.523327', NULL, NULL, NULL, NULL, 'Roma', '00139', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB739', '', 'Tradizionale', 'Poster', 'bifacciale', '', 'Roma', '', '', ' € 10.000 ', ' € 20.000 '),
(19, NULL, NULL, '3x2', NULL, NULL, 'Viale Jonio, 185', '41.944706', '12.534701', NULL, NULL, NULL, NULL, 'Roma', '00141', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB753', '', 'Tradizionale', 'Poster', 'bifacciale', '', '', '', '', ' € 10.000 ', ' € 20.000 '),
(20, NULL, NULL, '3x2', NULL, NULL, 'Viale Jonio, 44', '41.944461', '12.543305', NULL, NULL, NULL, NULL, 'Roma', '00141', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB757', '', 'Tradizionale', 'Poster', 'bifacciale', '', 'Roma', '', '', ' € 10.000 ', ' € 20.000 '),
(21, NULL, NULL, '3x2', NULL, NULL, 'Via dei Prati Fiscali, 213', '41.946345', '12.518534', NULL, NULL, NULL, NULL, 'Roma', '00139', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB763', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(22, NULL, NULL, '3x2', NULL, NULL, 'Via Prenestina, 1072', '41.893370', '12.611979', NULL, NULL, NULL, NULL, 'Roma', '00155', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB827', '', 'Tradizionale', 'Poster', 'monofacciale', '', '', '', '', ' € 5.000 ', ' € 10.000 '),
(23, NULL, NULL, '3x2', NULL, NULL, 'Via Prenestina, 601', '41.893968', '12.583088', NULL, NULL, NULL, NULL, 'Roma', '00155', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB829', '', 'Tradizionale', 'Poster', 'opaco', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(24, NULL, NULL, '3x2', NULL, NULL, 'Via Achille Vertunni, 26', '41.905847', '12.614205', NULL, NULL, NULL, NULL, 'Roma', '00155', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB852', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(25, NULL, NULL, '3x2', NULL, NULL, 'Via di Tor Tre Teste, 125', '41.877248', '12.588896', NULL, NULL, NULL, NULL, 'Roma', '00169', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB869', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(26, NULL, NULL, '3x2', NULL, NULL, 'Via Populonia, 36', '41.876673', '12.506151', NULL, NULL, NULL, NULL, 'Roma', '00183', NULL, NULL, 'PUBBLI LEVEL', 'MG Marketing Agency', 'BB969', '', 'Tradizionale', 'Poster', 'monofacciale', '', 'Roma', '', '', ' € 5.000 ', ' € 10.000 '),
(28, NULL, NULL, '3x2', NULL, NULL, 'Via Como 25', '45.5893507', '8.9111801', NULL, NULL, NULL, NULL, 'Milano', '20025', NULL, NULL, 'Adriano Cipriani', 'Adriano Cipriani', 'AC8741', 'AC8741', 'LED', 'Futuristico', '2', 'Legnano', 'Milano', 'Strada', 'Nuovo circuito', '800', '1200'); 

and this is the content of campagne_cliente table, you can see the id 44:

INSERT INTO `campagne_cliente` (`id_campagna_cliente`, `cliente_id_campagna`, `impianto_id_campagna`, `nome_campagna_cliente`) VALUES
(48, 3, 44, ''); 

and this is the content of campagne table, you can see the id of campagne_cliente.id_campagna_cliente 48:

INSERT INTO `campagne` (`id_campagna`, `data_inizio`, `data_fine`) VALUES
(48, '2018-12-12', '2018-12-13');

Solution

  • SELECT campagne_cliente.impianto_id_campagna, COUNT(campagne_cliente.impianto_id_campagna)
    FROM campagne_cliente
    WHERE campagne_cliente.impianto_id_campagna NOT IN
        (SELECT store_locator.id FROM store_locator WHERE store_locator.id IS NOT NULL)
    GROUP BY campagne_cliente.impianto_id_campagna