Related
Suppose we have a table in mySQL database where fname has a connection to another fname(BB_Connection_name), we would like have a query to find the pair(s) of friends who find connection among themselves.
E.g
Sidharth and Asim both have each others BBid and BB_Connection_ID
I have looked for similar case of father, son and grandson question but in that not each father has a son and thus inner joining them makes things easier for solving. I tried using that but didn't work.
Here i need to check BB_Connection_ID for every fname(A) and then corresponding fname has A's BBid as his BB_Connection_ID or not.
The pairs which would be chosen, should be like Sidharth<->Asim
We need to find the pairs who have their connection ID to each other.
==========================================================================
Code for recreation of the table:
-----------------------------------------------------------------------------
create table world.bigbb(
BBid int not null auto_increment,
fname varchar(20) NOT NULL,
lname varchar(30),
BBdays int not null,
No_of_Nom int,
BB_rank int not null,
BB_Task varchar(10),
BB_Connection_ID int,
BB_Connection_name varchar(10),
primary key (BBid)
);
insert into world.bigbb (fname, lname, BBdays, No_of_Nom, BB_rank, BB_Task, BB_Connection_ID, BB_Connection_name)
values
('Sidharth', 'Shukla', 40, 4, 2, 'Kitchen', 11, 'Asim'),
('Arhaan', 'Khan', 7, 1, 9, 'Kitchen', 16, 'Rashmi'),
('Vikas', 'Bhau', 7, 1, 8, 'Bedroom', 11, 'Asim'),
('Khesari', 'Bihari', 7, 1, 12, 'Kitchen', 9, 'Paras'),
('Tehseem', 'Poonawala', 7, 1, 11, 'Washroom', 12, 'Khesari'),
('Shehnaaz', 'Gill', 40, 4, 4, 'Washroom', 9, 'Paras'),
('Himanshi', 'Khurana', 7, 0, 7, 'Bedroom', 8, 'Shefali'),
('Shefali', 'Zariwala', 7, 1, 10, 'Bedroom', 1, 'Sidharth'),
('Paras', 'Chabra', 40, 3, 1, 'Bathroom', 10, 'Mahira'),
('Mahira', 'Sharma', 40, 4, 5, 'Kitchen', 9, 'Paras'),
('Asim', 'Khan', 40, 3, 3, 'Bathroom', 1, 'Sidharth'),
('Arti', 'Singh', 40, 5, 6, 'Captain', 1, 'Sidharth'),
('Sidharth', 'Dey', 35, 6, 16, 'None', 14, 'Shefali'),
('Shefali', 'Bagga', 38, 5, 15, 'None', 13, 'Sidharth'),
('Abu', 'Fifi', 22, 5, 17, 'None', 11, 'Asim'),
('Rashmi', 'Desai', 38, 5, 13, 'None', 17, 'Debolina'),
('Debolina', 'Bhattacharjee', 38, 5, 14, 'None', 16, 'Rashmi');
One solution would be to self-join the table:
select
b1.fname name1,
b2.fname name2
from bigbb b1
inner join bigbb b2
on b1.BB_Connection_ID = b2.BBid
and b2.BB_Connection_ID = b1.BBid
and b1.BBid < b2.BBid
This will give you one record for each pair, with the record having the smallest BBid in the first column.
This demo on DB Fiddle with your sample data returns:
name1 | name2
:------- | :-------
Sidharth | Asim
Paras | Mahira
Sidharth | Shefali
Rashmi | Debolina
I am sure that this question has already been answered, I just don't know what I am looking for.
What I've got are price lists that update every month and I am looking for a query that lists all items (tnr) that increase in price by over 20%
In this case I'd like to the the "tnr"
136234194430
832124069830
183078059150
I could loop through all items that I know that there is a smarter, faster, more elegant way to do it
The dummy table to test stuff on
CREATE TABLE `pricelist` (
`tnr` bigint(64) NOT NULL,
`price` double NOT NULL,
`discount` int(8) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `pricelist` (`tnr`, `price`, `discount`, `date`) VALUES
(183078059150, 33.89, 26, '2018-08-01'),
(514780535750, 78.73, 19, '2018-08-01'),
(121475122010, 521.54, 16, '2018-08-01'),
(726576581300, 168.36, 10, '2018-08-01'),
(832124069830, 22.69, 50, '2018-08-01'),
(342122275110, 131.5, 26, '2018-08-01'),
(345067567690, 6.34, 26, '2018-08-01'),
(121113618790, 195.5, 16, '2018-08-01'),
(511681969780, 291.74, 23, '2018-08-01'),
(411372385450, 129.75, 23, '2018-08-01'),
(15097806600, 46.68, 19, '2018-08-01'),
(613592995940, 259.47, 19, '2018-08-01'),
(135414163780, 17, 19, '2018-08-01'),
(726076671410, 68.91, 11, '2018-08-01'),
(136234194430, 36.86, 23, '2018-08-01'),
(541122685800, 10.25, 16, '2018-08-01'),
(514722202230, 83.19, 23, '2018-08-01'),
(125177976530, 257.12, 26, '2018-08-01'),
(114377922120, 19.18, 23, '2018-08-01'),
(642169317400, 2.54, 26, '2018-08-01'),
(14085256200, 16.44, 14, '2018-08-01'),
(114313045460, 22.46, 16, '2018-08-01'),
(331014284930, 1042.02, 19, '2018-08-01'),
(183078059150, 53.89, 26, '2018-09-01'),
(514780535750, 78.73, 19, '2018-09-01'),
(121475122010, 521.54, 16, '2018-09-01'),
(726576581300, 168.36, 10, '2018-09-01'),
(832124069830, 42.69, 50, '2018-09-01'),
(342122275110, 131.5, 26, '2018-09-01'),
(345067567690, 6.34, 26, '2018-09-01'),
(121113618790, 195.5, 16, '2018-09-01'),
(511681969780, 291.74, 23, '2018-09-01'),
(411372385450, 129.75, 23, '2018-09-01'),
(15097806600, 46.68, 19, '2018-09-01'),
(613592995940, 259.47, 19, '2018-09-01'),
(135414163780, 17, 19, '2018-09-01'),
(726076671410, 68.91, 11, '2018-09-01'),
(136234194430, 66.86, 23, '2018-09-01'),
(541122685800, 10.25, 16, '2018-09-01'),
(514722202230, 83.19, 23, '2018-09-01'),
(125177976530, 257.12, 26, '2018-09-01'),
(114377922120, 19.18, 23, '2018-09-01'),
(642169317400, 2.54, 26, '2018-09-01'),
(14085256200, 16.44, 14, '2018-09-01'),
(114313045460, 22.46, 16, '2018-09-01'),
(331014284930, 1042.02, 19, '2018-09-01');
Thanks a lot
Determine a "forward price" for each tnr and date (new price on a next date), utilizing Correlated subquery.
Using Derived tables and grouping on tnr, filter (using Having clause) out the tnr having "growth" greater than 20%
Use the following query (SQL Fiddle Demo):
SELECT inner_nest.tnr, 100*(inner_nest.forward_price - inner_nest.price)/inner_nest.price as growth
FROM
(
SELECT t1.*, (SELECT t2.price
FROM pricelist AS t2
WHERE t2.tnr = t1.tnr
AND t2.date > t1.date
ORDER BY t2.date ASC LIMIT 1) AS forward_price
FROM pricelist AS t1
) AS inner_nest
GROUP BY inner_nest.tnr
HAVING growth > 20
I am trying to write a query wherein it should count the number of students and tell me the remaining seats available in a vehicle.
Have managaed to identify which student is associated to which bus but getting stuck to find the seat remaining
Below is data :
vehnum route seats student id
23 2 45 2345
33 3 46 6789
Below is the query :
SELECT deveh.vehicle_reg_no AS vehnum
, veh.route_code AS route
, deveh.seating_capacity AS vehseat
, class.fk_stu_id
FROM tbl_stu_class AS class
JOIN tbl_stu_route AS route
ON route.fk_stu_cls_id = class.pk_stu_cls_id
JOIN list_routes AS veh
ON route.fk_route_id = veh.pk_route_id
JOIN list_vehicles AS deveh
ON deveh.pk_vehicle_id = veh.fk_vehicle_id
WHERE class.fk_year_id = 62
AND class.current_yr = 'Y'
Added sample data :
INSERT INTO `list_vehicles` (`pk_vehicle_id`, `vehicle_reg_no`, `vehicle_type`, `regd_owner_name`, `seating_capacity`, `brand_model`, `type_of_body`, `reg_address`, `fuel_type`, `chasis_no`, `reg_authority`, `engine_no`, `color`, `reg_date`, `reg_valid_date`, `month_yr_mfg`, `fk_user_id`, `timestamp`) VALUES
(46, 'J58987', 'Bus', 'M', 30, 'VOlvo', 'Steel', 'FBD', 'Petrol', '565', 'M1', '5689', 'blue', '2016-10-02', '2016-10-02', '2014-12-31', 1, '2018-07-11 18:01:06'),
(53, 'J1234', 'Bus', 'der', 45, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '123456', 'det', '2365', 'blue', '2010-12-12', '2020-12-12', '2009-12-11', 1, '2018-07-12 06:54:50'),
(54, 'J1234er', 'Van', 'der', 46, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '12345634', 'det', '236534', 'blue', '2020-02-03', '2020-02-03', '2008-11-11', 1, '2018-07-12 06:57:59');
INSERT INTO `tbl_stu_class` (`pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`, `fk_user_id`, `timestamp`) VALUES
(1, 56, 50, 22, 10, 'N', 1, '2018-06-08 06:57:34'),
(3, 123, 50, 24, 7, 'N', 1, '2018-06-12 07:54:46'),
(4, 126, 50, 24, 7, 'N', 56, '2018-06-12 07:54:46'),
(5, 123, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(6, 126, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(7, 132, 50, 22, 9, 'Y', 1, '2018-06-24 10:27:57'),
(8, 133, 51, 23, NULL, 'Y', 1, '2018-06-24 18:22:33'),
(10, 127, 51, 23, NULL, 'Y', 0, '2018-07-11 17:47:05'),
(11, 134, 62, 22, NULL, 'Y', 0, '2018-07-13 08:11:16'),
(12, 135, 62, 21, 7, 'Y', 1, '2018-07-13 11:12:08'),
(13, 136, 62, 21, 9, 'Y', 1, '2018-07-13 14:59:04');
I'm trying to make an average list for a bowling club. I have a database with the following tables:
team_medlem (medlem_id, name, address, etc)
team_samling (match_id, lag_id, borta (0 = home game, 1 = away game), date, time, etc)
team_match (id, match_id, s1, s2, etc)
team_resultat (id, medlem_id, results, series, banp, match_id)
I want to get the total average, average home and away average. result is the total points for the entire game for each player and the series is the number of played series.
I have this:
SELECT team_resultat.medlem_id, team_medlem.namn, sum(resultat)/sum(serier) as snitt, sum(banp)
FROM team_resultat
JOIN team_medlem ON team_resultat.medlem_id = team_medlem.medlem_id
JOIN team_samling ON team_resultat.match_id = team_samling.match_id
WHERE datum >= current_date - interval '1' year
GROUP BY namn
ORDER BY snitt desc
How can I get home average and away average?
I have tried this but it miscalculated averages
SELECT team_resultat.medlem_id, team_medlem.namn, sum(resultat)/sum(serier) as snitt, sum(banp), avg(case when not borta then resultat/serier end) as hemmasnitt, avg(case when borta then resultat/serier end) as bortasnitt
FROM team_resultat
JOIN team_medlem ON team_resultat.medlem_id = team_medlem.medlem_id
JOIN team_samling ON team_resultat.match_id = team_samling.match_id
WHERE datum >= current_date - interval '1' year
GROUP BY namn
ORDER BY snitt desc
Sampeldata:
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:07
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_medlem`
--
CREATE TABLE IF NOT EXISTS `team_medlem` (
`medlem_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`namn` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
PRIMARY KEY (`medlem_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Medlems register'AUTO_INCREMENT=36 ;
--
-- Dumpning av Data i tabell `team_medlem`
--
INSERT INTO `team_medlem` (`medlem_id`, `namn`) VALUES
(1, 'Sven-Åke Jansson'),
(2, 'Christer Wendel'),
(4, 'Sören Carlsson'),
(5, 'Jan Ingvarsson'),
(6, 'Lars-Göran Wetterholm'),
(7, 'Anders Svensson'),
(8, 'Bengt Carlsson'),
(9, 'Per-Olof Johansson'),
(10, 'Barsom Calan'),
(11, 'Mikael Mårtensson'),
(12, 'Andreas Johansson'),
(13, 'Jonas Wendel'),
(14, 'Sören Fransson'),
(15, 'Daniel Fransson'),
(16, 'Stefan Lord'),
(18, 'Lennart Johansson'),
(19, 'Jonas Nilsson'),
(20, 'Mikael Nilsson'),
(21, 'Patrik Emanuelsson'),
(22, 'Jörgen Norman'),
(24, 'Anders Johansson'),
(25, 'Andreas Larsson'),
(26, 'Roger Larsson'),
(27, 'Peter Ericson'),
(28, 'Dado Hrnic'),
(29, 'Maria Lord-Johansson'),
(33, 'Mats Wellermark');
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:13
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_samling`
--
CREATE TABLE IF NOT EXISTS `team_samling` (
`match_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lag_id` int(11) DEFAULT NULL,
`omg` int(2) unsigned DEFAULT NULL,
`borta` tinyint(1) unsigned DEFAULT '0',
`datum` date DEFAULT NULL,
`tid` time DEFAULT NULL,
`motstandare` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
`samling` varchar(6) COLLATE utf8_swedish_ci DEFAULT NULL,
`ovrigt` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
PRIMARY KEY (`match_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Samling' AUTO_INCREMENT=14 ;
--
-- Dumpning av Data i tabell `team_samling`
--
INSERT INTO `team_samling` (`match_id`, `lag_id`, `omg`, `borta`, `datum`, `tid`, `motstandare`, `samling`, `ovrigt`) VALUES
(1, 1, 1, 0, '2014-10-28', '12:15:00', 'Forsheda', '11:30', 'match dräkt'),
(2, 2, 1, 0, '2014-10-27', '10:00:00', 'Jönköping KK', '09:15', ''),
(3, 3, 3, 0, '2014-10-30', '15:20:00', 'Alvesta', '14:30', ''),
(4, 2, 2, 1, '2014-11-05', '12:00:00', 'Sävsjö', '09:15', 'Buss'),
(5, 2, 2, 1, '2014-11-05', '14:00:00', 'Eksjö', NULL, NULL),
(6, 3, 2, 0, '2014-11-06', '11:15:00', 'Jönköping kk', '10:30', NULL),
(11, 1, 20, 1, '2014-11-05', '12:30:00', 'test borta 2', '09:30', NULL),
(13, 1, 22, 0, '2014-11-06', '10:00:00', 'Test hemma', '09:00', NULL);
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:16
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_resultat`
--
CREATE TABLE IF NOT EXISTS `team_resultat` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`medlem_id` int(11) unsigned DEFAULT NULL,
`resultat` decimal(5,2) unsigned DEFAULT NULL,
`serier` int(2) unsigned DEFAULT NULL,
`banp` int(1) unsigned DEFAULT NULL,
`match_id` int(11) unsigned DEFAULT NULL,
`resultat_sparad` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Match resultat' AUTO_INCREMENT=53 ;
--
-- Dumpning av Data i tabell `team_resultat`
--
INSERT INTO `team_resultat` (`id`, `medlem_id`, `resultat`, `serier`, `banp`, `match_id`, `resultat_sparad`) VALUES
(1, 6, '800.00', 4, 3, 2, '2014-10-11 17:11:37'),
(2, 7, '790.00', 4, 3, 2, '2014-10-11 17:11:37'),
(3, 11, '780.00', 4, 2, 2, '2014-10-11 17:11:37'),
(4, 12, '770.00', 4, 2, 2, '2014-10-11 17:11:37'),
(5, 14, '760.00', 4, 3, 2, '2014-10-11 17:11:37'),
(6, 15, '750.00', 4, 2, 2, '2014-10-11 17:11:37'),
(7, 24, '740.00', 4, 2, 2, '2014-10-11 17:11:37'),
(8, 25, '505.00', 3, 1, 2, '2014-10-11 17:11:37'),
(9, 27, '165.00', 1, 0, 2, '2014-10-11 17:11:37'),
(10, 8, '700.00', 4, 2, 1, '2014-10-26 14:29:20'),
(11, 9, '690.00', 4, 3, 1, '2014-10-26 14:29:20'),
(12, 10, '680.00', 4, 2, 1, '2014-10-26 14:29:20'),
(13, 13, '670.00', 4, 2, 1, '2014-10-26 14:29:20'),
(14, 19, '660.00', 4, 2, 1, '2014-10-26 14:29:20'),
(15, 20, '650.00', 4, 1, 1, '2014-10-26 14:29:20'),
(16, 22, '640.00', 4, 2, 1, '2014-10-26 14:29:20'),
(17, 25, '640.00', 4, 1, 1, '2014-10-26 14:29:20'),
(19, 1, '800.00', 4, 2, 3, '2014-10-26 14:31:00'),
(20, 2, '790.00', 4, 2, 3, '2014-10-26 14:31:00'),
(21, 4, '780.00', 4, 2, 3, '2014-10-26 14:31:00'),
(22, 5, '770.00', 4, 2, 3, '2014-10-26 14:31:00'),
(23, 7, '760.00', 4, 2, 3, '2014-10-26 14:31:00'),
(24, 8, '750.00', 4, 2, 3, '2014-10-26 14:31:00'),
(25, 14, '740.00', 4, 3, 3, '2014-10-26 14:31:00'),
(26, 15, '500.00', 3, 1, 3, '2014-10-26 14:31:00'),
(27, 18, '150.00', 1, 1, 3, '2014-10-26 14:31:00'),
(28, 2, '800.00', 4, 2, 4, '2014-10-26 16:57:47'),
(29, 6, '790.00', 4, 2, 4, '2014-10-26 16:57:47'),
(30, 11, '780.00', 4, 2, 4, '2014-10-26 16:57:47'),
(31, 12, '770.00', 4, 2, 4, '2014-10-26 16:57:47'),
(32, 14, '760.00', 4, 2, 4, '2014-10-26 16:57:47'),
(33, 15, '750.00', 4, 2, 4, '2014-10-26 16:57:47'),
(34, 25, '740.00', 4, 2, 4, '2014-10-26 16:57:47'),
(35, 33, '730.00', 4, 2, 4, '2014-10-26 16:57:47'),
(36, 2, '800.00', 4, 2, 5, '2014-10-26 16:58:26'),
(37, 6, '790.00', 4, 2, 5, '2014-10-26 16:58:26'),
(38, 11, '780.00', 4, 2, 5, '2014-10-26 16:58:26'),
(39, 12, '770.00', 4, 2, 5, '2014-10-26 16:58:26'),
(40, 14, '760.00', 4, 2, 5, '2014-10-26 16:58:26'),
(41, 15, '750.00', 4, 2, 5, '2014-10-26 16:58:26'),
(42, 25, '740.00', 4, 2, 5, '2014-10-26 16:58:26'),
(43, 33, '730.00', 4, 2, 5, '2014-10-26 16:58:26'),
(44, 10, '700.00', 4, 2, 6, '2014-10-26 16:59:15'),
(45, 13, '690.00', 4, 2, 6, '2014-10-26 16:59:15'),
(46, 16, '680.00', 4, 2, 6, '2014-10-26 16:59:15'),
(47, 19, '670.00', 4, 2, 6, '2014-10-26 16:59:15'),
(48, 18, '660.00', 4, 2, 6, '2014-10-26 16:59:15'),
(49, 21, '650.00', 4, 2, 6, '2014-10-26 16:59:15'),
(50, 26, '640.00', 4, 2, 6, '2014-10-26 16:59:15'),
(51, 27, '520.00', 3, 2, 6, '2014-10-26 16:59:15'),
(52, 29, '160.00', 1, 2, 6, '2014-10-26 16:59:15');
expected results
Namn Snitt Hemma Borta Banp
Sven-Åke Jansson 200,00 200,00 0 2
Christer Wendel 199,17 197,50 200,00 6
Lars-Göran Wetterholm 198,33 200,00 197,50 7
Mikael Mårtensson 195,00 195,00 195,00 6
Sören Carlsson 195,00 195,00 0 2
Anders Svensson 193,75 193,75 0 5
Andreas Johansson 192,50 192,50 192,50 6
Jan Ingvarsson 192,50 192,50 0 2
Sören Fransson 188,75 187,50 190,00 10
Anders Johansson 185,00 185,00 0 2
Daniel Fransson 183,33 178,57 187,50 7
Mats Wellermark 182,50 0 182,50 4
Bengt Carlsson 181,25 181,25 0 4
Andreas Larsson 175,00 163,57 185,00 6
Barsom Calan 172,50 172,50 0 4
Per-Olof Johansson 172,50 172,50 0 3
Peter Ericson 171,25 171,25 0 2
Jonas Wendel 170,00 170,00 0 4
Stefan Lord 170,00 170,00 0 2
Jonas Nilsson 166,25 166,25 0 4
Mikael Nilsson 162,50 162,50 0 1
Patrik Emanuelsson 162,50 162,50 0 2
Lennart Johansson 162,00 162,00 0 3
Jörgen Norman 160,00 160,00 0 2
Maria Lord-Johansson 160,00 160,00 0 2
Roger Larsson 160,00 160,00 0 2
I have a query that tries to find all shopping carts containing a set of given packages.
For each package I join the corresponding cartitem table once, because I am only interested in carts containing all given packages.
When I reach more than 15 packages(joins) the query performance rapidly drops.
I have two indeces on the corresponding foreign columns and am aware that mysql uses only one of them. When I add an index over the 2 columns(cartitem_package_id,cartitem_cart_id) it works, but is this the only way to solve this situation?
I would like to know why MYSQL suddently stucks in this situation and what may be the mysql internal problem, because I do not see any deeper problem with this definition and query? Does that may be an issue with the query optimizer and can I do something(e.g. adding brackets) to support or force a specific query execution? Or has anyone a different approach here, using another query?
The query looks something like this:
SELECT cart_id
FROM cart
INNER JOIN cartitem as c1 ON cart_id=c1.cartitem_cart_id AND c1.cartitem_package_id= 7
INNER JOIN cartitem as c2 ON cart_id=c2.cartitem_cart_id AND c2.cartitem_package_id= 8
INNER JOIN cartitem as c3 ON cart_id=c3.cartitem_cart_id AND c3.cartitem_package_id= 9
INNER JOIN cartitem as c4 ON cart_id=c4.cartitem_cart_id AND c4.cartitem_package_id= 10
INNER JOIN cartitem as c5 ON cart_id=c5.cartitem_cart_id AND c5.cartitem_package_id= 11
INNER JOIN cartitem as c6 ON cart_id=c6.cartitem_cart_id AND c6.cartitem_package_id= 12
INNER JOIN cartitem as c7 ON cart_id=c7.cartitem_cart_id AND c7.cartitem_package_id= 13
INNER JOIN cartitem as c8 ON cart_id=c8.cartitem_cart_id AND c8.cartitem_package_id= 14
INNER JOIN cartitem as c9 ON cart_id=c9.cartitem_cart_id AND c9.cartitem_package_id= 15
INNER JOIN cartitem as c10 ON cart_id=c10.cartitem_cart_id AND c10.cartitem_package_id= 16
INNER JOIN cartitem as c11 ON cart_id=c11.cartitem_cart_id AND c11.cartitem_package_id= 17
INNER JOIN cartitem as c12 ON cart_id=c12.cartitem_cart_id AND c12.cartitem_package_id= 18
INNER JOIN cartitem as c13 ON cart_id=c13.cartitem_cart_id AND c13.cartitem_package_id= 19
INNER JOIN cartitem as c14 ON cart_id=c14.cartitem_cart_id AND c14.cartitem_package_id= 20
INNER JOIN cartitem as c15 ON cart_id=c15.cartitem_cart_id AND c15.cartitem_package_id= 21
INNER JOIN cartitem as c16 ON cart_id=c16.cartitem_cart_id AND c16.cartitem_package_id= 22
INNER JOIN cartitem as c17 ON cart_id=c17.cartitem_cart_id AND c17.cartitem_package_id= 23
Output:
No result.
Consider the following sample structure:
CREATE TABLE IF NOT EXISTS `cart` (
`cart_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`cart_state` smallint(20) DEFAULT NULL,
PRIMARY KEY (`cart_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=80 ;
INSERT INTO `cart` (`cart_id`, `cart_state`) VALUES
(1, 0),(2, 5),(3, 0),(4, 0),(5, 0),(6, 0),(7, 0),(8, 0),(9, 0),(10, 0),(11, 0),(12, 0),(13, 0),(14, 5),(15, 5),(16, 10),(17, 0),(18, 10),(19, 40),(20, 10),(21, 5),(22, 0),(23, 10),(24, 10),(25, 0),(26, 10),(27, 5),(28, 5),(29, 0),(30, 5),(31, 0),(32, 0),(33, 0),(34, 0),(35, 0),(36, 0),(37, 0),(38, 0),(39, 0),(40, 0),(41, 0),(42, 0),(43, 0),(44, 0),(45, 40),(46, 0),(47, 0),(48, 1),(49, 0),(50, 5),(51, 0),(52, 0),(53, 5),(54, 5),(55, 0),(56, 0),(57, 10),(58, 0),(59, 0),(60, 5),(61, 0),(62, 0),(63, 10),(64, 0),(65, 5),(66, 5),(67, 10),(68, 10),(69, 0),(70, 0),(71, 10),(72, 0),(73, 10),(74, 0),(75, 10),(76, 0),(77, 10),(78, 0),(79, 10);
CREATE TABLE IF NOT EXISTS `cartitem` (
`cartitem_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`cartitem_package_id` int(10) unsigned DEFAULT NULL,
`cartitem_cart_id` int(10) unsigned DEFAULT NULL,
`cartitem_price` decimal(7,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`cartitem_id`),
KEY `cartitem_package_id` (`cartitem_package_id`),
KEY `cartitem_cart_id` (`cartitem_cart_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=89 ;
INSERT INTO `cartitem` (`cartitem_id`, `cartitem_package_id`, `cartitem_cart_id`, `cartitem_price`) VALUES
(1, 4, 2, 200.00),(2, 7, 3, 30.00),(3, 14, 9, 255.00),(4, 14, 9, 255.00),(5, 22, 9, 120.00),(6, 22, 9, 120.00),(7, 13, 13, 300.00),(8, 13, 13, 300.00),(9, 7, 14, 450.00),(10, 13, 14, 250.00),(11, 17, 14, 150.00),(12, 7, 15, 450.00),(13, 13, 15, 250.00),(14, 18, 15, 127.50),(15, 7, 16, 450.00),(16, 17, 16, 150.00),(17, 7, 18, 450.00),(18, 7, 19, 450.00),(19, 17, 19, 150.00),(20, 21, 19, 25.00),(21, 13, 20, 300.00),(22, 7, 21, 550.00),(23, 19, 21, 105.00),(24, 22, 21, 120.00),(25, 17, 22, 150.00),(26, 7, 23, 550.00),(27, 11, 24, 245.00),(31, 7, 26, 450.00),(32, 21, 26, 25.00),(33, 21, 26, 25.00),(34, 22, 26, 120.00),(35, 23, 26, 120.00),(36, 10, 27, 382.50),(37, 22, 27, 120.00),(38, 13, 27, 250.00),(39, 10, 28, 297.50),(43, 7, 29, 550.00),(41, 20, 28, 82.50),(42, 22, 28, 120.00),(44, 7, 30, 550.00),(46, 22, 30, 120.00),(47, 23, 30, 120.00),(48, 21, 18, 25.00),(49, 21, 19, 25.00),(50, 17, 37, 150.00),(51, 17, 37, 150.00),(52, 21, 37, 25.00),(53, 21, 37, 25.00),(54, 4, 45, 1.20),(55, 6, 45, 0.00),(56, 7, 47, 450.00),(57, 4, 50, 200.00),(58, 13, 52, 250.00),(59, 13, 19, 300.00),(60, 9, 19, 0.00),(61, 17, 53, 150.00),(62, 7, 53, 450.00),(63, 22, 18, 120.00),(64, 7, 16, 450.00),(65, 7, 54, 450.00),(66, 7, 57, 450.00),(67, 17, 57, 150.00),(68, 7, 56, 450.00),(69, 17, 59, 150.00),(70, 7, 60, 450.00),(71, 17, 61, 150.00),(72, 17, 63, 150.00),(73, 21, 65, 25.00),(74, 7, 66, 450.00),(75, 7, 67, 450.00),(76, 11, 68, 385.00),(77, 7, 71, 450.00),(78, 11, 73, 385.00),(79, 13, 73, 300.00),(80, 4, 75, 200.00),(82, 7, 73, 30.00),(83, 18, 73, 127.50),(84, 23, 73, 120.00),(85, 7, 73, 30.00),(86, 10, 77, 382.50),(87, 7, 79, 550.00),(88, 17, 79, 150.00);
The given query was a possible edge case leading to no results in this example.
SELECT cart_id
FROM cart
INNER JOIN cartitem as c1 ON cart_id=c1.cartitem_cart_id AND c1.cartitem_package_id= 7
INNER JOIN cartitem as c3 ON cart_id=c3.cartitem_cart_id AND c3.cartitem_package_id= 9
INNER JOIN cartitem as c4 ON cart_id=c4.cartitem_cart_id AND c4.cartitem_package_id= 13
INNER JOIN cartitem as c5 ON cart_id=c5.cartitem_cart_id AND c5.cartitem_package_id= 17
INNER JOIN cartitem as c6 ON cart_id=c6.cartitem_cart_id AND c6.cartitem_package_id= 21
Output:
cart_id
-------------
19
19
The query should return all carts containing items that are connected to packages(7,9,13,17,21) in this case.
My approach to your problem would be:
SELECT
cart_id
FROM
cart
INNER JOIN
cartitem
ON
cart_id = cartitem_cart_id
WHERE
cartitem_package_id IN (7,9,13,17,21) -- items that got to be in the cart
GROUP BY
cart_id
HAVING
count(distinct cartitem_package_id) = 5 -- number of different packages
;
DEMO with your data
Explanation
The principle is to filter first with the list of the desired values, here your packages. Now count the different packages per cart (GROUP BY cart_id). If this count matches the number of values in your filter list, then every single package must be in this cart.
You can replace the value list of the IN clause with a subselect, if you get those values from a subselect.
You should see that this approach should be easy to adapt to similar needs.