Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I only want to display results based on a certain match criteria (ONLY the results that follows that pattern)

I expect a result set with ID 1 to 5, and 12-16 only as shown below

ID	W1	W2	W3	W4	W5
1	1	43	72	10	33
2	61	38	16	68	51
3	80	54	62	87	45
4	73	39	64	77	22
5	49	82	32	56	41

12	1	43	72	10	33
13	61	38	16	68	51
14	80	54	62	87	45
15	73	39	64	77	22
16	49	82	32	56	41


What I have tried:

SQL
SELECT T1.ID, T1.W1, T1.W2, T1.W3, T1.W4, T1.W5

FROM drvdb.scores t1
JOIN drvdb.scores t2 ON t1.ID = t2.ID

WHERE (T1.W1 ='1' AND T1.W2 = '43' AND T1.W3 = '72' AND T1.W4 = '10' AND T1.W5 = '33')
OR (T2.W1 ='61' AND T2.W2 = '38' AND T2.W3 = '16' AND T2.W4 = '68' AND T2.W5 = '51')
OR (T2.W1 ='80' AND T2.W2 = '54' AND T2.W3 = '62' AND T2.W4 = '87' AND T2.W5 = '45')
OR (T2.W1 ='73' AND T2.W2 = '39' AND T2.W3 = '64' AND T2.W4 = '77' AND T2.W5 = '22')
OR (T2.W1 ='49' AND T2.W2 = '82' AND T2.W3 = '32' AND T2.W4 = '56' AND T2.W5 = '41')

GROUP BY T1.ID
HAVING COUNT (DISTINCT T1.W1, T1.W2, T1.W3, T1.W4, T1.W5) = 1	


I have tried several queries, I still end up with the same undesired results
My problem is with the conditional OR, how I wish I could make it AND that’s where I’m stucked and can’t think further

Isn’t there a way around this? Is this impossible? I have been stuck on this for the past two months trying to figure out how best I can do this...my back just aches.
Now is time to call for help.
I’m new to SQL and trying to get acquainted this language


Any endeavor would be much appreciated

I Use MySQL Community Server - GPL version 8.0.14

This is my script file

SQL
CREATE DATABASE IF NOT EXISTS `drvdb`;
USE `drvdb`;


CREATE TABLE IF NOT EXISTS `scores` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `W1` varchar(2) DEFAULT NULL,
  `W2` varchar(2) DEFAULT NULL,
  `W3` varchar(2) DEFAULT NULL,
  `W4` varchar(2) DEFAULT NULL,
  `W5` varchar(2) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `W1` (`W1`),
  KEY `W2` (`W2`),
  KEY `W3` (`W3`),
  KEY `W4` (`W4`),
  KEY `W5` (`W5`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;



INSERT INTO `scores` (`ID`, `W1`, `W2`, `W3`, `W4`, `W5`) VALUES
	(1, '1', '43', '72', '10', '33'),
	(2, '61', '38', '16', '68', '51'),
	(3, '80', '54', '62', '87', '45'),
	(4, '73', '39', '64', '77', '22'),
	(5, '49', '82', '32', '56', '41'),
	(6, '68', '8', '30', '35', '1'),
	(7, '20', '21', '22', '23', '24'),
	(8, '61', '38', '16', '68', '51'),
	(9, '1', '43', '72', '10', '33'),
	(10, '80', '54', '62', '87', '45'),
	(11, '49', '82', '32', '56', '41'),
	(12, '1', '43', '72', '10', '33'),
	(13, '61', '38', '16', '68', '51'),
	(14, '80', '54', '62', '87', '45'),
	(15, '73', '39', '64', '77', '22'),
	(16, '49', '82', '32', '56', '41'),
	(17, '61', '38', '16', '68', '51'),
	(18, '80', '54', '62', '87', '45'),
	(19, '49', '82', '32', '56', '41'),
	(20, '61', '38', '16', '68', '51');
Posted
Updated 3-Jun-19 20:07pm
v5

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900