I have 4 different tables:
Contest
Object
Contest_Obj
Winning
CONTEST has many objects (through contest_obj) AND winnings. A contest contains start and end dates.
OBJECT has many contests (through contest_obj)
CONTEST_OBJ contains object_id, contest_id, votes (most votes after end date of contest is a winner)
WINNING has contest_id, amount, and amount_type (dollars and euros)
I would like to be able to access this data in as few SQL calls as possible. More specifically, I am not currently storing the winner of a contest, so I have been calculating this on the fly.
I was hoping to get some help writing the SQL for:
total_winning_dollars (return total dollars won in past contests)
total_winning_euros (return total dollars won in past contests)
all_won_contests (return all contests that object is the winner along with winnings for that contest as both dollars and euros)
Code I have to determine winner of a contest:
SELECT "objects".*
FROM "objects"
INNER JOIN "contest_objs" ON "objects"."id" = "contest_objs"."object_id"
WHERE "contest_objs"."contest_id" = ?
ORDER BY votes DESC
LIMIT 1
sample table data:
CREATE TABLE `contests` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`start_date` DATETIME,
`end_date` DATETIME,
`user_id` INT,
`title` STRING
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `objects` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` STRING
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `winnings` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`contest_id` INT NOT NULL,
`comment` TEXT,
`amount_type` STRING NOT NULL,
`amount` INT NOT NULL,
`user_id` DATETIME
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `contest_objs` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`contest_id` INT NOT NULL,
`object_id` INT NOT NULL,
`votes` INT
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO `objects` VALUES
(1, "Foo"),
(2, "Bar")
INSERT INTO `contests` VALUES
(1, "2012-09-16 23:30:16.220991", "2012-09-17 23:30:16.220991", 1, "Contest X"),
(2, "2012-09-17 23:30:16.220991", "2016-09-17 23:30:16.220991", 2, "Contest Y")
INSERT INTO `winnings` VALUES
(1, 1, "Giving $5", 5, "dollars", 1),
(2, 1, "Giving 2 euros", 2, "euros", 1),
(3, 1, "Giving 4 euros", 4, "euros", 2),
(4, 2, "Giving 2 euros to different contest", 2, "euros", 1)
INSERT INTO `contest_objs` VALUES
(1, 1, 1, 10),
(2, 1, 2, 12),
(3, 2, 2, 0),
(4, 2, 2, 0)
In the above example:
Contest X (over) has 2 objects in it (foo and bar). Bar is the winner of Contest X with 12 votes. Bar has won $5 dollars and 6 euros total from this contest.
Related
I am trying to get all bills that have NOT been FULLY paid.
I have three tables that are needed for this.
Table 1 - billInvoiceMain
biId - unique ID
userId - users ID
type - bill or invoice
userItemId - unique ID that user chooses for their records
Table 2 - billInvoiceDetail
biId - references unique ID in billInvoiceMain
quantity
price
Table 3 - transaction
transactionId - unique ID
userId - users ID
biId - references id in billInvoiceMain
paymentAmount
So a user enters bills, and then once they make a payment (multiple smaller payments could be made on a bill until it reaches the full amount or they could make a single payment for the whole amount) they enter it and it gets saved in the transaction table.
Here is a SQL Fiddle that has abbreviated versions of test data.
CREATE TABLE IF NOT EXISTS `billInvoiceDetail` (
`biId` int(15) NOT NULL,
`productId` int(15) DEFAULT NULL,
`accountId` int(15) DEFAULT NULL,
`description` varchar(2000) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`quantity` decimal(20,3) NOT NULL,
`price` decimal(20,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `billInvoiceDetail` (`biId`, `productId`, `accountId`, `description`, `quantity`, `price`) VALUES
(51, NULL, 7, 'Pylaisiella steerei Ando & Higuchi', 4.000, 19.65),
(51, NULL, 11, 'Rubus insons L.H. Bailey', 1.000, 10.17),
(99, NULL, 11, 'Leontodon hispidus L.', 3.000, 11.99),
(99, NULL, 7, 'Peltophorum (T. Vogel) Benth.', 5.000, 33.76),
(100, NULL, 8, 'Scleria P.J. Bergius', 1.000, 10.55),
(100, NULL, 12, 'Gilia ochroleuca M.E. Jones ssp. exilis (A. Gray) A.D. Grant & V.E. Grant', 2.000, 42.54);
CREATE TABLE IF NOT EXISTS `billInvoiceMain` (
`biId` int(15) NOT NULL,
`userId` int(15) NOT NULL,
`type` varchar(7) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`cvId` int(15) NOT NULL,
`startDate` date DEFAULT NULL,
`dueDate` date DEFAULT NULL,
`userItemId` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `billInvoiceMain` (`biId`, `userId`, `type`, `cvId`, `startDate`, `dueDate`, `userItemId`) VALUES
(51, 1, 'bill', 17, '2021-01-01', '2021-01-31', '53396841'),
(99, 1, 'bill', 28, '2021-01-01', '2021-01-31', '16269083'),
(100, 1, 'bill', 28, '2021-01-07', '2021-01-17', '03200283');
CREATE TABLE IF NOT EXISTS `transaction` (
`transactionId` int(15) NOT NULL,
`userId` int(15) NOT NULL,
`biId` int(15) NOT NULL,
`paymentDate` date NOT NULL,
`paymentMethod` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`accountId` int(15) NOT NULL,
`paymentAmount` decimal(20,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `transaction` (`transactionId`, `userId`, `biId`, `paymentDate`, `paymentMethod`, `accountId`, `paymentAmount`) VALUES
(51, 1, 51, '2021-01-04', 'Check', 1, 78.60),
(52, 1, 51, '2021-01-19', 'Credit Card', 3, 10.17),
(53, 1, 99, '2021-01-14', 'Check', 1, 10.00);
SELECT billInvoiceMain.biId, SUM(transaction.paymentAmount), billInvoiceMain.useritemid
FROM billInvoiceMain
INNER JOIN transaction ON billInvoiceMain.biId = transaction.biId
WHERE billInvoiceMain.userId = 1 AND billInvoiceMain.type = 'bill'
GROUP BY billInvoiceMain.biId;
SELECT ROUND(ABS(SUM(billInvoiceDetail.price *billInvoiceDetail.quantity)),2)
FROM billInvoiceDetail
INNER JOIN billInvoiceMain ON billInvoiceDetail.biId = billInvoiceMain.biId
WHERE billInvoiceMain.userId=1 AND billInvoiceMain.type = 'bill'
GROUP BY billInvoiceMain.biId;
SELECT billInvoiceMain.biId, billInvoiceMain.useritemid
FROM billInvoiceMain
INNER JOIN transaction ON billInvoiceMain.biId = transaction.biId
INNER JOIN billInvoiceDetail ON billInvoiceDetail.biId = transaction.biId
WHERE billInvoiceMain.userId = 1 AND billInvoiceMain.type = 'bill'
HAVING SUM(transaction.paymentAmount) != ROUND(ABS(SUM(billInvoiceDetail.price *billInvoiceDetail.quantity)),2);
The first query allows me to sum of all the payments from transaction grouped by bill id.
The second query sums all the bills.
The third query I tried combing the two. However, when I try to use a GROUP BY, it gives an error. So, I got rid of that and now it just returns the first bill even if it has been paid.
Desired Results (retrieves the biId and userItemId of all bills that have not been fully paid based on the transaction table):
biId
userItemId
99
16269083
100
03200283
I have spent a lot of time trying to figure this out but am lost.
The following query retrieves rows that don't match the biId obtained by joining the result of sum of all the payments from transaction grouped by bill id and the result of sum of all the bills from billInvoiceDetail grouped by bill id.
SELECT biId, useritemid FROM billInvoiceMain
WHERE userId = 1 AND type = 'bill'
AND biId NOT IN(
SELECT t.biId FROM
(SELECT biId,SUM(paymentAmount) pay FROM transaction GROUP BY biId) t
INNER JOIN
(SELECT biId,ROUND(ABS(SUM(price*quantity)),2) bill FROM billInvoiceDetail GROUP BY biId) d
ON t.biId=d.biId AND t.pay=d.bill
)
SQL Fiddle
I have these tables:
DROP TABLE IF EXISTS books;
CREATE TABLE `books` (
`bookId` mediumint(8) UNSIGNED NOT NULL,
`title` varchar(10) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `books`
ADD PRIMARY KEY (`bookId`),
DROP TABLE IF EXISTS movements;
CREATE TABLE `movements` (
`movementId` mediumint(8) UNSIGNED NOT NULL,
`movementTypeId` tinyint(3) UNSIGNED NOT NULL,
`deletedFlag` tinyint(3) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `movements`
ADD PRIMARY KEY (`movementId`),
ADD KEY `movementId` (`movementTypeId`,`deletedFlag`) USING BTREE;
DROP TABLE IF EXISTS movements_types;
CREATE TABLE `movements_types` (
`movementTypeId` mediumint(8) UNSIGNED NOT NULL,
`title` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `movements_types`
ADD PRIMARY KEY (`movementTypeId`);
DROP TABLE IF EXISTS movements_books;
CREATE TABLE `movements_books` (
`movementId` mediumint(8) UNSIGNED NOT NULL,
`bookId` mediumint(8) UNSIGNED NOT NULL,
`bookSize` tinyint(3) UNSIGNED NOT NULL,
`quantity` smallint(5) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `books` (`bookId`, `title`) VALUES
(1, 'Harry Potter'),
(2, 'Mysql Join'),
(3, 'Comedy');
INSERT INTO `movements` (`movementId`, `movementTypeId`, `deletedFlag`) VALUES
(7, 1, 0),
(8, 2, 0),
(9, 2, 0);
INSERT INTO `movements_types` (`movementTypeId`, `title`) VALUES
(1, "Bought"),
(2, "Sold");
INSERT INTO `movements_books` (`movementId`, `bookId`, `bookSize`, `quantity`) VALUES
(7, 1, 1, 3),
(7, 1, 2, 3),
(7, 2, 1, 3),
(7, 2, 2, 3),
(8, 1, 1, 2),
(8, 1, 1, 2),
(9, 2, 1, 3);
bookSize is just an integer indicating the size of the book.
movements_books actually has 111824 records, movements has 4534.
deletedFlag is 1 for a deleted movement (I prefer to keep them flagged) and 0 otherwise.
I need help creating indexes and I need these results:
1) sizes sold/bought/available for each book (bookId, bookSize, booksSold, booksBought, booksBought minus booksSold)
2) books sold/bought/available for each book of all size (bookId, booksSold, booksBought, booksBought minus booksSold)
To get the one of the statistics I tried:
SELECT
books.title
movements_books_grouped.bookId,
SUM(IF(movements.movementTypeId=1, movements_books_grouped.quantity, NULL)) AS booksBought,
SUM(IF(movements.movementTypeId=2, movements_books_grouped.quantity, NULL)) AS booksSold,
(
SUM(IF(movements.movementTypeId=1, movements_books_grouped.quantity, 0))-
SUM(IF(movements.movementTypeId=2, movements_books_grouped.quantity, 0))
) AS booksAvailability,
FROM
(
SELECT bookId,quantity,movementId FROM movements_books GROUP BY bookId
) AS movements_books_grouped
JOIN movements ON movements.movementId=movements_books_grouped.movementId
JOIN books ON books.bookId=movements_books_grouped.bookId
GROUP BY movements_books_grouped.bookId
ORDER BY book.title
but it's very slow.
Edit: I needed to add another table to the example because that's the one that makes really slow the last query. I need to join to this table because I need to order the result by title.
Here's a query that can use indexes:
SELECT m.movementId
, m.movementTypeId
, m.deletedFlag
, mb.bookId
, mb.bookSize
, t.title
FROM movements m
JOIN movements_types t
ON t.movementTypeId = m.movementTypeId
JOIN movements_books mb
ON mb.movementid = m.movementid
WHERE m.deletedFlag=0;
An index on (m.movementTypeId, m.deletedFlag) may prove beneficial, but best to suck it and see.
i have got the below query which references couple of views 'goldedRunQueries' and 'currentGoldMarkings'. My issue seems to be from the view that is referred in the subquery - currentGoldMarkings. While execution, MySQL first materializes this subquery and then implements the where clauses of 'queryCode' and 'runId', which therefore results in execution time of more than hour as the view refers tables that has got millions of rows of data. My question is how do I enforce those two where conditions on the subquery before it materializes.
SELECT goldedRunQueries.queryCode, goldedRunQueries.runId
FROM goldedRunQueries
LEFT OUTER JOIN
( SELECT measuredRunId, queryCode, COUNT(resultId) as c
FROM currentGoldMarkings
GROUP BY measuredRunId, queryCode
) AS accuracy ON accuracy.measuredRunId = goldedRunQueries.runId
AND accuracy.queryCode = goldedRunQueries.queryCode
WHERE goldedRunQueries.queryCode IN ('CH001', 'CH002', 'CH003')
and goldedRunQueries.runid = 5000
ORDER BY goldedRunQueries.runId DESC, goldedRunQueries.queryCode;
Here are the two views. Both of these also get used in a standalone mode and so integrating any clauses into them is not possible.
CREATE VIEW currentGoldMarkings
AS
SELECT result.resultId, result.runId AS measuredRunId, result.documentId,
result.queryCode, result.queryValue AS measuredValue,
gold.queryValue AS goldValue,
CASE result.queryValue WHEN gold.queryValue THEN 1 ELSE 0 END AS correct
FROM results AS result
INNER JOIN gold ON gold.documentId = result.documentId
AND gold.queryCode = result.queryCode
WHERE gold.isCurrent = 1
CREATE VIEW goldedRunQueries
AS
SELECT runId, queryCode
FROM runQueries
WHERE EXISTS
( SELECT 1 AS Expr1
FROM runs
WHERE (runId = runQueries.runId)
AND (isManual = 0)
)
AND EXISTS
( SELECT 1 AS Expr1
FROM results
WHERE (runId = runQueries.runId)
AND (queryCode = runQueries.queryCode)
AND EXISTS
( SELECT 1 AS Expr1
FROM gold
WHERE (documentId = results.documentId)
AND (queryCode = results.queryCode)
)
)
Note: The above query reflects only a part of my actual query. There are 3 other left outer joins which are similar in nature to the above subquery which makes the problem far more worse.
EDIT: As suggested, here is the structure and some sample data for the tables
CREATE TABLE `results`(
`resultId` int auto_increment NOT NULL,
`runId` int NOT NULL,
`documentId` int NOT NULL,
`queryCode` char(5) NOT NULL,
`queryValue` char(1) NOT NULL,
`comment` varchar(255) NULL,
CONSTRAINT `PK_results` PRIMARY KEY
(
`resultId`
)
);
insert into results values (100, 242300, 'AC001', 'I', NULL)
insert into results values (100, 242300, 'AC001', 'S', NULL)
insert into results values (150, 242301, 'AC005', 'I', 'abc')
insert into results values (100, 242300, 'AC001', 'I', NULL)
insert into results values (109, 242301, 'PQ001', 'S', 'zzz')
insert into results values (400, 242400, 'DD006', 'I', NULL)
CREATE TABLE `gold`(
`goldId` int auto_increment NOT NULL,
`runDate` datetime NOT NULL,
`documentId` int NOT NULL,
`queryCode` char(5) NOT NULL,
`queryValue` char(1) NOT NULL,
`comment` varchar(255) NULL,
`isCurrent` tinyint(1) NOT NULL DEFAULT 0,
CONSTRAINT `PK_gold` PRIMARY KEY
(
`goldId`
)
);
insert into gold values ('2015-02-20 00:00:00', 138904, 'CH001', 'N', NULL, 1)
insert into gold values ('2015-05-20 00:00:00', 138904, 'CH001', 'N', 'aaa', 1)
insert into gold values ('2016-02-20 00:00:00', 138905, 'CH002', 'N', NULL, 0)
insert into gold values ('2015-12-12 00:00:00', 138804, 'CH001', 'N', 'zzzz', 1)
CREATE TABLE `runQueries`(
`runId` int NOT NULL,
`queryCode` char(5) NOT NULL,
CONSTRAINT `PK_runQueries` PRIMARY KEY
(
`runId`,
`queryCode`
)
);
insert into runQueries values (100, 'AC001')
insert into runQueries values (109, 'PQ001')
insert into runQueries values (400, 'DD006')
CREATE TABLE `runs`(
`runId` int auto_increment NOT NULL,
`runName` varchar(63) NOT NULL,
`isManual` tinyint(1) NOT NULL,
`runDate` datetime NOT NULL,
`comment` varchar(1023) NULL,
`folderName` varchar(63) NULL,
`documentSetId` int NOT NULL,
`pipelineVersion` varchar(50) NULL,
`isArchived` tinyint(1) NOT NULL DEFAULT 0,
`pipeline` varchar(50) NULL,
CONSTRAINT `PK_runs` PRIMARY KEY
(
`runId`
)
);
insert into runs values ('test1', 0, '2015-08-04 06:30:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
insert into runs values ('test2', 1, '2015-12-04 12:30:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
insert into runs values ('test3', 1, '2015-06-24 10:56:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
insert into runs values ('test4', 1, '2016-05-04 11:30:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
First, let's try to improve the performance via indexes:
results: INDEX(runId, queryCode) -- in either order
gold: INDEX(documentId, query_code, isCurrent) -- in that order
After that, update the CREATE TABLEs in the question and add the output of:
EXPLAIN EXTENDED SELECT ...;
SHOW WARNINGS;
What version are you running? You effectively have FROM ( SELECT ... ) JOIN ( SELECT ... ). Before 5.6, neither subquery had an index; with 5.6, an index is generated on the fly.
It is a shame that the query is built that way, since you know which one to use: and goldedRunQueries.runid = 5000.
Bottom Line: add the indexes; upgrade to 5.6 or 5.7; if that is not enough, then rethink the use of VIEWs.
I has looking for a way to sort items in a mysql table.
Here a simplified version of the table
sqlfiddle => http://sqlfiddle.com/#!2/78521b/3/0
CREATE TABLE IF NOT EXISTS `test_sort` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sort` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `sort` (`sort`)
);
INSERT INTO `test_sort` (`id`,`sort`, `name`) VALUES
(1, 1, 'Joker'),
(2, 3, 'Queen of Spade'),
(3, 6, 'King of Heart'),
(4, 4, 'Ace of Diamond'),
(5, 17, 'Three of Clubs'),
(6, 60, 'Seven of Clubs'),
(7, 2, 'Ten of Spades'),
(8, 5, 'Ace of Heart');
So once the items (cards) has been sorted by the user i want to run the query on the sort column so it remains consistent.
Solution found here : MySQL update a field with an incrementing variable
SET #n=0;
UPDATE `test_sort` SET `sort` = #n := #n + 1 ORDER BY `sort`
QUESTION: how this query would act (performance wise) if it was used on thousands (or millions) of records?
Don't store sort in the table; store it in a separate table. Furthermore, don't UPDATE that table, recreate it. Further-furthermore, use the following to avoid any downtime:
CREATE TABLE New SELECT ... -- generate new sort order
RENAME TABLE Sort TO Old, New To Sort;
DROP TABLE Old;
Following is a dump of the tables and data needed to answer understand the system:-
The system consists of tutors and classes.
The data in the table All_Tag_Relations stores tag relations for each tutor registered and each class created by a tutor. The tag relations are used for searching classes.
CREATE TABLE IF NOT EXISTS `Tags` (
`id_tag` int(10) unsigned NOT NULL auto_increment,
`tag` varchar(255) default NULL,
PRIMARY KEY (`id_tag`),
UNIQUE KEY `tag` (`tag`),
KEY `id_tag` (`id_tag`),
KEY `tag_2` (`tag`),
KEY `tag_3` (`tag`),
KEY `tag_4` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(1, 'Sandeepan'),
(2, 'Nath'),
(3, 'first'),
(4, 'class'),
(5, 'new'),
(6, 'Bob'),
(7, 'Cratchit');
CREATE TABLE IF NOT EXISTS `All_Tag_Relations` (
`id_tag` int(10) unsigned NOT NULL default '0',
`id_tutor` int(10) default NULL,
`id_wc` int(10) unsigned default NULL,
KEY `All_Tag_Relations_FKIndex1` (`id_tag`),
KEY `id_wc` (`id_wc`),
KEY `id_tag` (`id_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `All_Tag_Relations` (`id_tag`, `id_tutor`, `id_wc`) VALUES
(1, 1, NULL),
(2, 1, NULL),
(3, 1, 1),
(4, 1, 1),
(6, 2, NULL),
(7, 2, NULL),
(5, 2, 2),
(4, 2, 2),
(8, 1, 3),
(9, 1, 3);
Following is my query:-
This query searches for "first class" (tag for first = 3 and for class = 4, in Tags table) and returns all those classes such that both the terms first and class are present in the class name.
SELECT wtagrels.id_wc,SUM(DISTINCT( wtagrels.id_tag =3)) AS
key_1_total_matches,
SUM(DISTINCT( wtagrels.id_tag =4)) AS
key_2_total_matches
FROM all_tag_relations AS wtagrels
WHERE ( wtagrels.id_tag =3
OR wtagrels.id_tag =4 )
GROUP BY wtagrels.id_wc
HAVING key_1_total_matches = 1
AND key_2_total_matches = 1
LIMIT 0, 20
And it returns the class with id_wc = 1.
But, I want the search to show all those classes such that all the search terms are present in the class name or its tutor name
So that searching "Sandeepan class" (wtagrels.id_tag = 1,4) or "Sandeepan Nath" also returns the class with id_wc=1. And Searching. Searching "Bob First" should not return any classes.
Please modify the above query or suggest a new query, if possible using MyIsam - fulltext search, but somehow help me get the result.
I think this query would help you:
SET #tag1 = 1, #tag2 = 4; -- Setting some user variables to see where the ids go. (you can put the values in the query)
SELECT wtagrels.id_wc,
SUM(DISTINCT( wtagrels.id_tag =#tag1 OR wtagrels.id_tutor =#tag1)) AS key_1_total_matches,
SUM(DISTINCT( wtagrels.id_tag =#tag2 OR wtagrels.id_tutor =#tag2)) AS key_2_total_matches
FROM all_tag_relations AS wtagrels
WHERE ( wtagrels.id_tag =#tag1 OR wtagrels.id_tag =#tag2 )
GROUP BY wtagrels.id_wc
HAVING key_1_total_matches = 1 AND key_2_total_matches = 1
LIMIT 0, 20
It returns id_wc = 1.
For (6, 3) the query returns nothing.