I have to write a complex query to get products in MySQL with multiple conditions and stucking on relations with multiple conditions.
The database structure is built from Typo3. At the moment I have 4 joins with multiple conditions. Everything is working fine but the last line of my SQL is the problem. But I can't get any solution.
SELECT p.uid
FROM tx_myext_domain_model_product p
JOIN tx_myext_domain_model_productwirkung w
ON p.uid = w.product
JOIN sys_category_record_mm ck
ON p.uid = ck.uid_foreign
JOIN sys_category_record_mm cc
ON p.uid = cc.uid_foreign
JOIN sys_category_record_mm cw
ON w.uid = cw.uid_foreign
WHERE p.season IN(1,'')
AND p.type = "herbizid"
AND ck.uid_local = 2
AND ck.tablenames = "tx_myext_domain_model_product"
AND ck.fieldname = "culture"
AND cc.tablenames = "tx_myext_domain_model_product"
AND cc.fieldname = "chemicals"
AND cw.tablenames = "tx_myext_domain_model_productwirkung"
AND cw.uid_local IN (2, 395, 257)
AND w.grad = 6;
# and wirkung with cw.uid_local = 2 should have w.grad = 6
# and wirkung with cw.uid_local = 395 should have w.grad = 6
# and wirkung with cw.uid_local = 257 should have w.grad in range from 1 to 6
The 'productwirkung' should have the category 2, 395 and 257 and the 'productwirkung' with the category 2 and 395 should have the grad 6.
I know, with this last line I can't get anything...
EDIT
I cannot add w.grad = 6 because the product should have a productwirkung with the category 257 too with grad 1 - 6. One product has multiple relations with productwirkung. So if there is a product with productwirkung 2 with grad = 6, productwirkung 395 with grad = 6 and productwirkung 257 with grad = 3 I want this product. If I ask for ... cw.uid_local IN (2, 395, 257) AND w.grad = 6 The product wont be selected because productwirkung 257 doesn't has w.grad = 6.
Assuming uid_local is unique, it can not by definition be both 2 and 395
I guess you wanted to go for
...
AND ((`cw`.`uid_local` = 2 AND `w`.`grad` = 6) OR (`cw`.`uid_local` = 395 AND `w`.`grad` = 6));
EDIT
Since in both sub-expressions you're checking for w.grad = 6 this might also be simplified into
...
AND w.grad = 6
AND (cw.uid_local = 2 OR cw.uid_local = 395)
Or even further simplified into
...
AND w.grad = 6
AND cw.uid_local IN (2, 395)
Your problem is that your last two where conditions are conflicting. They state that a row has to contain the cw.uid_local of either 2, 395 or 257 AND must have the ID 2 AND 395 in a single row. If you rewrite the last two lines as an OR conditional, you should get the results you're looking for:
SELECT `p`.`uid`
FROM `tx_myext_domain_model_product` `p`
INNER JOIN `tx_myext_domain_model_productwirkung` `w`
ON `p`.`uid` = w.product
INNER JOIN `sys_category_record_mm` `ck`
ON `ck`.`tablenames` = "tx_myest_domain_model_product"
AND `p`.`uid` = ck.uid_foreign
AND `ck`.`fieldname` = "culture"
INNER JOIN `sys_category_record_mm` `cc`
ON `cc`.`tablenames` = "tx_myext_domain_model_product"
AND `p`.`uid` = cc.uid_foreign
AND `cc`.`fieldname` = "chemicals"
INNER JOIN `sys_category_record_mm` `cw`
ON `cw`.`tablenames` = "tx_myext_domain_model_productwirkung"
AND `w`.`uid` = cw.uid_foreign
WHERE ((`p`.`season` = "1") OR (`p`.`season` = ""))
AND (`p`.`type` = "herbizid")
AND (`ck`.`uid_local` = "2")
AND ((`cw`.`uid_local` IN (2, 395) AND `w`.`grad` = 6) OR `cw`.`uid_local` = 257);
EDIT
Upon reading your edit, I think you'd best be served with subqueries, seeing as your required conditions are conflicting for a single query.
If you want the products where ALL conditions are met (products with "children" that have a (uid_local of 2 AND grad of 6) AND "children" with (uid_local of 395 AND grad of 6) AND "children" with (uid_local of 257 AND grad between 1 and 6)), I would suggest using inner joins with subqueries to constrict your data to the "child cases":
SELECT DISTINCT p.uid
FROM tx_myext_domain_model_product p
### WHERE cw.uid_local = 2 AND w.grad = 6
JOIN (
SELECT p_1.uid
FROM tx_myext_domain_model_product p_1
JOIN tx_myext_domain_model_productwirkung w_1
ON p_1.uid = w_1.product
JOIN sys_category_record_mm cw_1
ON w_1.uid = cw_1.uid_foreign
WHERE cw_1.tablenames = "tx_myext_domain_model_productwirkung"
AND cw_1.uid_local = 2
AND w_1.grad = 6
) p1 ON p1.uid = p.uid
### WHERE cw.uid_local = 395 AND w.grad = 6
JOIN (
SELECT p_2.uid
FROM tx_myext_domain_model_product p_2
JOIN tx_myext_domain_model_productwirkung w_2
ON p_2.uid = w_2.product
JOIN sys_category_record_mm cw_2
ON w_2.uid = cw_2.uid_foreign
WHERE cw_2.tablenames = "tx_myext_domain_model_productwirkung"
AND cw_2.uid_local = 395
AND w_2.grad = 6
) p2 ON p2.uid = p.uid
### WHERE cw.uid_local = 257 AND w.grad BETWEEN 1 AND 6
JOIN (
SELECT p_3.uid
FROM tx_myext_domain_model_product p_3
JOIN tx_myext_domain_model_productwirkung w_3
ON p_3.uid = w_3.product
JOIN sys_category_record_mm cw_3
ON w_3.uid = cw_3.uid_foreign
WHERE cw_3.tablenames = "tx_myext_domain_model_productwirkung"
AND cw_3.uid_local = 257
AND w_3.grad BETWEEN 1 AND 6
) p3 ON p3.uid = p.uid
JOIN tx_myext_domain_model_productwirkung w
ON p.uid = w.product
JOIN sys_category_record_mm ck
ON p.uid = ck.uid_foreign
JOIN sys_category_record_mm cc
ON p.uid = cc.uid_foreign
JOIN sys_category_record_mm cw
ON w.uid = cw.uid_foreign
WHERE p.season IN(1,'')
AND p.type = "herbizid"
AND ck.uid_local = 2
AND ck.tablenames = "tx_myest_domain_model_product"
AND ck.fieldname = "culture"
AND cc.tablenames = "tx_myext_domain_model_product"
AND cc.fieldname = "chemicals";
If we rewrite your query, we get (amongst other things) the following:
AND cw.uid_local = 2
AND cw.uid_local = 395
Unless uid_local occupies some sort of quantum state, this is unlikely to be successful.
FWIW, I find this (slightly) easier to read...
SELECT p.uid
FROM tx_myext_domain_model_product p
JOIN tx_myext_domain_model_productwirkung w
ON p.uid = w.product
JOIN sys_category_record_mm ck
ON p.uid = ck.uid_foreign
JOIN sys_category_record_mm cc
ON p.uid = cc.uid_foreign
JOIN sys_category_record_mm cw
ON w.uid = cw.uid_foreign
WHERE p.season IN(1,'')
AND p.type = "herbizid"
AND ck.uid_local = 2
AND ck.tablenames = "tx_myest_domain_model_product"
AND ck.fieldname = "culture"
AND cw.uid_local IN (2, 395, 257)
AND cw.tablenames = "tx_myext_domain_model_productwirkung"
AND cc.tablenames = "tx_myext_domain_model_product"
AND cc.fieldname = "chemicals"
AND w.grad = 6;
Related
I building a custom SQL query of a product name with id_product, id_lang, id_manufacturer, category, meta_description, brand name and multiple feature ids. This SQL query shows no result.
Here's my query:
SELECT pl.id_product, pl.id_lang, ml.id_manufacturer, p.active, pl.name as name_product, fp.id_feature as name_attribute, cl.name as category, m.name as brand, pl.meta_description
FROM pr_product p
LEFT JOIN pr_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN pr_category_lang cl ON (cl.id_category = p.id_category_default and cl.id_lang = pl.id_lang)
LEFT JOIN pr_lang l on (l.id_lang = pl.id_lang)
LEFT JOIN pr_manufacturer_lang ml on (l.id_lang = pl.id_lang and l.id_lang = ml.id_lang)
LEFT JOIN pr_manufacturer m on (ml.id_manufacturer = m.id_manufacturer)
LEFT JOIN pr_feature_product fp on (pl.id_product = fp.id_product)
Where l.active = 1 AND pl.id_lang = 2 AND cl.name = 9 and fp.id_feature = 2 AND fp.id_feature = 3 AND fp.id_feature = 35 AND fp.id_feature = 39
Order by p.id_product, pl.id_lang, ml.id_manufacturer, fp.id_feature
The goal is to creat a new meta description in the spreadsheet and import it to the database. In php, the query would be easier, but I have zero knowledge about it.
I have query with many joins and I'm searching for optimization for it.
It's about computers:
For examples I have:
Lenovo 8gbRAM 1TB core i5 ips etc. (all these after brand name are attributes)
I have configuration where, I want to change attribute 8gbRAM to 16gbRAM and I have to search for other item with all these attributes and 16gbRAM
Two tables:
**st_item**
- id
- name
...
**st_item_specification_attribute**
- id
- st_item_id
- attribute_id
- attribute_value_id
...
My problem is that my item has 15 attributes. When I tested with lower number of attributes I use this structure of query and it works, but now system has 85k items and over 1kk item attributes
This is the query:
SELECT `st_item`.id FROM `st_item`
LEFT JOIN `st_item_specification_attribute` `sisa_36590` ON st_item.id = sisa_36590.item_id AND sisa_36590.attribute_id = 365
LEFT JOIN `st_item_specification_attribute` `sisa_367910` ON st_item.id = sisa_367910.item_id AND sisa_367910.attribute_id = 367
LEFT JOIN `st_item_specification_attribute` `sisa_374641` ON st_item.id = sisa_374641.item_id AND sisa_374641.attribute_id = 374
LEFT JOIN `st_item_specification_attribute` `sisa_378366` ON st_item.id = sisa_378366.item_id AND sisa_378366.attribute_id = 378
LEFT JOIN `st_item_specification_attribute` `sisa_382500` ON st_item.id = sisa_382500.item_id AND sisa_382500.attribute_id = 382
LEFT JOIN `st_item_specification_attribute` `sisa_372134` ON st_item.id = sisa_372134.item_id AND sisa_372134.attribute_id = 372
LEFT JOIN `st_item_specification_attribute` `sisa_41268` ON st_item.id = sisa_41268.item_id AND sisa_41268.attribute_id = 412
LEFT JOIN `st_item_specification_attribute` `sisa_413368` ON st_item.id = sisa_413368.item_id AND sisa_413368.attribute_id = 413
LEFT JOIN `st_item_specification_attribute` `sisa_414929` ON st_item.id = sisa_414929.item_id AND sisa_414929.attribute_id = 414
LEFT JOIN `st_item_specification_attribute` `sisa_418496` ON st_item.id = sisa_418496.item_id AND sisa_418496.attribute_id = 418
LEFT JOIN `st_item_specification_attribute` `sisa_385748` ON st_item.id = sisa_385748.item_id AND sisa_385748.attribute_id = 385
LEFT JOIN `st_item_specification_attribute` `sisa_36625` ON st_item.id = sisa_36625.item_id AND sisa_36625.attribute_id = 366
LEFT JOIN `st_item_specification_attribute` `sisa_366355` ON st_item.id = sisa_366355.item_id AND sisa_366355.attribute_id = 366
LEFT JOIN `st_item_specification_attribute` `sisa_366816` ON st_item.id = sisa_366816.item_id AND sisa_366816.attribute_id = 366
LEFT JOIN `st_item_specification_attribute` `sisa_366370` ON st_item.id = sisa_366370.item_id AND sisa_366370.attribute_id = 366
WHERE (`parent_id`=1032) AND
(sisa_36590.attribute_value_id = 2230) AND
(sisa_367910.attribute_value_id = 2451) AND
(sisa_374641.attribute_value_id = 3793) AND
(sisa_378366.attribute_value_id = 2955) AND
(sisa_382500.attribute_value_id = 3879) AND
(sisa_372134.attribute_value_id = 2780) AND
(sisa_41268.attribute_value_id = 3363) AND
(sisa_413368.attribute_value_id = 3373) AND
(sisa_414929.attribute_value_id = 3378) AND
(sisa_418496.attribute_value_id = 3844) AND
(sisa_385748.attribute_value_id = 3036) AND
(sisa_36625.attribute_value_id = 2315) AND
(sisa_366355.attribute_value_id = 2408) AND
(sisa_366816.attribute_value_id = 2412) AND
(sisa_366370.attribute_value_id = 2420)
Query must compare specific pair attribute_id => attribute_value_id, that's the reason my "ON clause" to be with item_id and attribute_id and specific alias
You can use aggregation:
select i.id
from st_item i join
st_item_specification_attribute sisa
ON sisa.item_id = i.item_id
where i.parent_id = 1032 and
(sisa.attribute_id, attribute_value_id) in ( (365, 2230), (367, 2451), . . .)
group by i.id
having count(*) = 15;
You can move your WHERE conditions into ON conditions
and change LEFT JOIN to INNER JOIN.
SELECT `st_item`.id FROM `st_item`
JOIN `st_item_specification_attribute` `sisa_36590`
ON st_item.id = sisa_36590.item_id AND sisa_36590.attribute_id = 365
AND sisa_36590.attribute_value_id = 2230
JOIN `st_item_specification_attribute` `sisa_367910`
ON st_item.id = sisa_367910.item_id AND sisa_367910.attribute_id = 367
AND sisa_367910.attribute_value_id = 2451
...
WHERE `parent_id`=1032
2nd approach
SELECT `st_item`.id FROM `st_item`
JOIN `st_item_specification_attribute` `sisa`
ON st_item.id = sisa.item_id AND
(
(sisa.attribute_id = 365 AND sisa.attribute_value_id = 2230)
OR
(sisa.attribute_id = 367 AND sisa.attribute_value_id = 2451)
...
)
WHERE `parent_id`=1032
GROUP BY `st_item`.id
HAVING COUNT(*) = 15
I cannot predict the performance, but I think you could make on subselect out of all that joins
(I presume atribute_id and Attribute_value_id pairs are unique per item_id)
SELECT `st_item`.id FROM `st_item`
WHERE (`parent_id`=1032) AND
15 = (SELECT COUNT(*) FROM st_item_specification_attribute attr
WHERE `st_item`.id = attr.item_id
AND ( attribute_id = 365 AND attribute_value_id = 2230 OR
...
)
I would use a UNION ALL approach for this. It's easy to change, and fairly simple to read. Performance should be pretty good too:
--A CTE so you only have to change parent_id in one place
--I believe not all mysql versions support this, though
--You could of course just select the parent_id in de UNION ALL and
-- use a single WHERE in the outer query.
WITH st_item_id AS
(
SELECT id
, attribute_id
, attribute_value_id
FROM st_item
WHERE parent_id = 1032
)
SELECT UA.id
FROM (
SELECT st_item.id
FROM st_item_id
INNER JOIN st_item_specification_attribute sisa_36590
ON st_item_id.id = sisa_36590.item_id
AND sisa_36590.attribute_id = 365
AND sisa_36590.attribute_value_id = 2230
UNION ALL
SELECT st_item.id
FROM st_item_id
INNER JOIN st_item_specification_attribute sisa_367910
ON st_item_id.id = sisa_367910.item_id
AND sisa_367910.attribute_id = 367
AND sisa_367910.attribute_value_id = 2451
) UA
When somebody tries to search for a ticket, as admin, I get an email with this error.
OsTicket v1.11.0-rc1 (e321982)
[SELECT SQL_CALC_FOUND_ROWS A1.`number` AS `number`, A1.`ticket_id` AS `ticket_id`, A1.`source` AS `source`, A1.`created` AS `created`, A8.`subject` AS `cdata__subject`, A1.`isoverdue` AS `isoverdue`, B0.`name` AS `user__name`, A8.`priority` AS `cdata__priority`, B1.`firstname` AS `staff__firstname`, B1.`lastname` AS `staff__lastname`, B2.`name` AS `team__name`, A1.`team_id` AS `team_id`, A1.`isanswered` = '1' AS `8MwwjGf`, (SELECT COUNT(R0.`id`) AS `count` FROM `ost_thread` Q7 JOIN `ost_ticket` Q8 ON (Q7.`object_type` = 'T' AND Q7.`object_id` = Q8.`ticket_id`) LEFT JOIN `ost_thread_entry` R0 ON (Q7.`id` = R0.`thread_id`) WHERE Q8.`ticket_id` = A1.`ticket_id` AND NOT R0.`flags` & 4 != 0) AS _thread_count, (SELECT COUNT(R1.`id`) AS `count` FROM `ost_thread` Q7 JOIN `ost_ticket` Q8 ON (Q7.`object_type` = 'T' AND Q7.`object_id` = Q8.`ticket_id`) LEFT JOIN `ost_thread_entry` R0 ON (Q7.`id` = R0.`thread_id`) LEFT JOIN `ost_attachment` R1 ON (R1.`type` = 'H' AND R0.`id` = R1.`object_id`) WHERE Q8.`ticket_id` = A1.`ticket_id` AND R1.`inline` = 0) AS _att_count, COALESCE(B1.`firstname`, B1.`lastname`, B2.`name`, 'zzz') AS `assignee` FROM `ost_ticket` A1 JOIN `ost_ticket_status` A2 ON (A1.`status_id` = A2.`id`) LEFT JOIN `ost_thread` A3 ON (A3.`object_type` = 'T' AND A1.`ticket_id` = A3.`object_id`) LEFT JOIN `ost_thread_referral` A4 ON (A3.`id` = A4.`thread_id`) LEFT JOIN `ost_staff` A5 ON (A4.`object_type` = 'S' AND A4.`object_id` = A5.`staff_id`) LEFT JOIN `ost_team` A6 ON (A4.`object_type` = 'E' AND A4.`object_id` = A6.`team_id`) LEFT JOIN `ost_department` A7 ON (A4.`object_type` = 'D' AND A4.`object_id` = A7.`id`) LEFT JOIN `ost_ticket__cdata` A8 ON (A1.`ticket_id` = A8.`ticket_id`) JOIN `ost_user` B0 ON (A1.`user_id` = B0.`id`) LEFT JOIN `ost_staff` B1 ON (A1.`staff_id` = B1.`staff_id`) LEFT JOIN `ost_team` B2 ON (A1.`team_id` = B2.`team_id`) JOIN (SELECT COALESCE(Z3.`object_id`, Z5.`ticket_id`, Z8.`ticket_id`) as `ticket_id`, SUM(MATCH (Z1.title, Z1.content) AGAINST ('black8' IN NATURAL LANGUAGE MODE)) AS `relevance` FROM `ost__search` Z1 LEFT JOIN `ost_thread_entry` Z2 ON (Z1.`object_type` = 'H' AND Z1.`object_id` = Z2.`id`) LEFT JOIN `ost_thread` Z3 ON (Z2.`thread_id` = Z3.`id` AND Z3.`object_type` = 'T') LEFT JOIN `ost_ticket` Z5 ON (Z1.`object_type` = 'T' AND Z1.`object_id` = Z5.`ticket_id`) LEFT JOIN `ost_user` Z6 ON (Z6.`id` = Z1.`object_id` and Z1.`object_type` = 'U') LEFT JOIN `ost_organization` Z7 ON (Z7.`id` = Z1.`object_id` AND Z7.`id` = Z6.`org_id` AND Z1.`object_type` = 'O') LEFT JOIN ost_ticket Z8 ON (Z8.`user_id` = Z6.`id`) WHERE MATCH (Z1.title, Z1.content) AGAINST ('black8' IN NATURAL LANGUAGE MODE) GROUP BY `ticket_id`) Z1 WHERE A1.`ticket_id` = Z1.`ticket_id` AND ((A2.`state` = 'open' AND (A1.`staff_id` = 7 OR A5.`staff_id` = 7 OR A1.`team_id` IN (5) OR A6.`team_id` IN (5))) OR A1.`dept_id` IN (1, 4, 5) OR A7.`id` IN (1, 4, 5)) GROUP BY A1.`number`, A1.`ticket_id`, A1.`source`, A1.`created`, A8.`subject`, A1.`isoverdue`, B0.`name`, A8.`priority`, B1.`firstname`, B1.`lastname`, B2.`name`, A1.`team_id` LIMIT 25]
Can't find FULLTEXT index matching the column list
---- Backtrace ----
0 (root)/include/mysqli.php(199): osTicket->logDBError('DB Error #1191', '[SELECT SQL_CAL...')
1 (root)/include/class.orm.php(3404): db_query('SELECT SQL_CALC...', true, true)
2 (root)/include/class.orm.php(3451): MySqlExecutor->execute()
3 (root)/include/class.orm.php(2063): MySqlExecutor->getArray()
4 (root)/include/class.orm.php(2013): HashArrayIterator->{closure}()
5 (root)/include/class.orm.php(1992): CallbackSimpleIterator->next()
6 (root)/include/class.orm.php(2001): CallbackSimpleIterator->rewind()
7 (root)/include/class.orm.php(1673): CallbackSimpleIterator->valid()
8 (root)/include/class.orm.php(1708): CachedResultSet->fillTo(1)
9 (root)/include/class.orm.php(1569): CachedResultSet->offsetGet(0)
10 (root)/include/class.orm.php(1437): QuerySet->offsetGet(0)
11 (root)/include/class.orm.php(1394): QuerySet->exists(true)
12 (root)/include/staff/templates/queue-tickets.tmpl.php(79): QuerySet->total()
13 (root)/scp/tickets.php(543): require_once('/var/www/html/u...')
14 {main}
I ran
ALTER TABLE `ost__search` ADD FULLTEXT(title,content);
And that fixed the problem.
I am looking to combine two MySQL queries so it returns the common results so I know I am not looking to use UNION on this one. I tried writing a subselect statement but that didn't work out
First query:
SELECT s.ses_id, h.page, m.question, m.answer FROM session s
INNER JOIN history h on h.ses_id = s.ses_id
INNER JOIN multiple m on m.ses_id = s.ses_id
WHERE m.question = 4 and m.answer = 3 and h.page = 4
Second query:
SELECT s.ses_id, h.page, m.question, m.answer FROM session s
INNER JOIN history h on h.ses_id = s.ses_id
INNER JOIN multiple m on m.ses_id = s.ses_id
WHERE m.question = 114 and m.answer = 1 and h.page = 114
Failed merge query:
SELECT s.ses_id FROM session s, multiple, history h
JOIN (
SELECT session.ses_id
FROM session, history, multiple
WHERE multiple.question = 114 and multiple.answer = 1 and history.page = 114 and history.ses_id = session.ses_id and multiple.ses_id=session.ses_id
) q1 ON q1.ses_id = s.ses_id
WHERE s.interview = 'lifestyle' and s.finished = 'y' and multiple.page=4 and multiple.answer = 3 and h.page = 4 and h.ses_id = s.ses_id and multiple.ses_id=s.ses_id
The multiple table contains questions and answers and I am looking to find the ids of those who have answered the two questions with those specific answers.
I realize this should be easy and I am most likely overthinking and/or missing something.
You simply need to join both the history and multiple tables an additional time each for the 2nd question/answer combination.
SELECT s.ses_id,
m1.question, m1.answer, h1.page,
m2.question, m2.answer, h2.page
FROM session s
INNER JOIN history h1
ON h1.ses_id = s.ses_id AND h1.page = 4
INNER JOIN multiple m1
ON m1.ses_id = s.ses_id
AND m1.question = 4 AND m1.answer = 3
INNER JOIN history h2
ON h2.ses_id = s.ses_id AND h2.page = 114
INNER JOIN multiple m2
ON m2.ses_id = s.ses_id
AND m2.question = 114 and m2.answer = 1
WHERE s.interview = 'lifestyle' and s.finished = 'y'
Below simply altered query may be useful.,
SELECT s.ses_id, h.page, m.question, m.answer FROM session s
INNER JOIN history h on h.ses_id = s.ses_id
INNER JOIN multiple m on m.ses_id = s.ses_id
WHERE
( m.question = 4 and m.answer = 3 and h.page = 4 )
OR
( m.question = 114 and m.answer = 1 and h.page = 114 )
Merge operation is not needed.
if you are looking for intersection of two queries then you can simply use IN set operator of mysql
SELECT s.ses_id, h.page, m.question, m.answer
FROM session s
INNER JOIN history h on h.ses_id = s.ses_id
INNER JOIN multiple m on m.ses_id = s.ses_id
WHERE m.question = 4 and m.answer = 3 and h.page = 4 and s.ses_id
IN
(
SELECT s.ses_id
FROM session s
INNER JOIN history h on h.ses_id = s.ses_id
INNER JOIN multiple m on m.ses_id = s.ses_id
WHERE m.question = 114 and m.answer = 1 and h.page = 114
)
I've got 2 tables: interviews & interview_keywords.
An interview has 5 sorted keywords. I need a list of interviews with specified keywords in the right positions of the sorted list. This is what I've got so far, which isn't working:
SELECT i.id,
i.title
FROM interviews AS i
LEFT JOIN interview_keywords AS ik ON i.id = ik.interview_id
WHERE i.cat_id = 1
AND ( (ik.keyword_id = 39 AND ik.sort = 1)
AND (ik.keyword_id = 33 AND ik.sort = 2)
AND (ik.keyword_id = 51 AND ik.sort = 3)
AND (ik.keyword_id = 96 AND ik.sort = 4)
AND (ik.keyword_id = 97 AND ik.sort = 5))
SELECT i.id, i.title
FROM interviews i
INNER JOIN interview_keywords ik1
ON ik1.interview.id = i.id
AND ik.keyword_id = 39
AND ik1.sort = 1
INNER JOIN interview_keywords ik2
ON ik2.interview.id = i.id
AND ik2.keyword_id = 33
AND ik2.sort = 2
INNER JOIN interview_keywords ik3
ON ik3.interview.id = i.id
AND ik3.keyword_id = 51
AND ik3.sort = 3
INNER JOIN interview_keywords ik4
ON ik4.interview.id = i.id
AND ik4.keyword_id = 96
AND ik4.sort = 4
INNER JOIN interview_keywords ik5
ON ik5.interview.id = i.id
AND ik5.keyword_id = 97
AND ik5.sort = 5
WHERE i.cat_id = 1