Reputation: 1661
Let's imagine we have a small MySQL database:
Women:
women_shoes:
Shoes:
woman_handbags:
Handbags:
There are 2 relations many-to-many. What I want to get from such DB is: select all women which have shoes: nike, puma, mango and handbags: versace. I am interested in women who have every presented pair of shoes and every handbag. I know names of stuff and I want to find out the names.
Upvotes: 0
Views: 142
Reputation: 1
A try to do something like that - but i have problem...
i want to make bibliography records - i have a table where i store the datas of the selected articles (bibl_1983) - and i have tables where i store the themes (keywords) and names (persons mentioned in the article) - i try to convert a real bibliography into online searchable something - so i want to keep the original data system - to be compatible with the original printed one...
i could easily do if i want to show only one thing - for example the keywords or the persons
Tables are:
bibl_1983:
pub_id
,
author
,
title
,
periodical
,
date
,
volume
,
issue
,
page
,
illustration
,
annotation
contable_name:
contname_id
,
name_id2
,
pub_id4
namespace:
name_id
,
name
contable_subject:
cont_id
,
sub_id2
,
pub_id3
subject:
sub_id
,
subject
SELECT bibl_1983.pub_id, group_concat(subject.subject
ORDER BY subject.subject ASC) AS subject
FROM bibl_1983, subject, contable_subject
WHERE ((bibl_1983.pub_id=contable_subject.pub_id3) AND
(subject.sub_id=contable_subject.sub_id2))
GROUP BY bibl_1983.pub_id
it is works fine, and great and this is too:
SELECT bibl_1983.pub_id, group_concat(namespace.name
ORDER BY namespace.name ASC) AS name
FROM bibl_1983, namespace, contable_name
WHERE ((bibl_1983.pub_id=contable_name.pub_id4) AND
(namespace.name_id=contable_name.name_id2))
GROUP BY bibl_1983.pub_id
But i couldnt find out, how can i show the two things in the same time, in the same way, on the same page.
It is two different many to many connections.
What i want to show: article datas + keywords + persons in one query (or view).
Upvotes: 0
Reputation: 25842
Maybe something like this? if i understand your question correctly.
SELECT
r.*
FROM recipe r
JOIN recipe_ingredients ri ON ri.id_recipe = r.id_recipe
JOIN ingredients i ON i.id_ingredient = ri.id_ingredient
JOIN recipes_tags rt ON rt.id_recipe = r.id_recipe
JOIN tags t ON t.id_tag = rt.id_tag
WHERE i.name = 'ziemniaki'
OR i.name = 'cebula'
AND t.tag = "tani"
OR t.tag = "łatwy"
GROUP BY r.id_recipe
HAVING COUNT(r.id_recipe) > 3 -- all 4 of the criteria have been met
;
see working FIDDLE for clarification
Basically what this does is it returns a row when one of the four criteria is met. along with that it also will only return recipes that have at least one of the ingredients and at least one of the tags. so when 4 (or more) rows are returned then the criteria is met for a recipe with the requested params
Upvotes: 1
Reputation: 32392
Since the 2nd criteria only has 1 handbag you don't really need to select from a subquery but I figured it might change to multiple handbags.
SELECT
w.*
FROM women w
JOIN
(
SELECT
id_woman
FROM woman_shoes ws
JOIN shoes s ON ws.id_shoe = s.id_shoe
WHERE s.name IN ('puma','nike','mango')
GROUP BY id_woman
HAVING COUNT(*) = 3
) has_shoes hs ON hs.id_woman = w.id_woman
JOIN
(
SELECT
id_woman
FROM woman_handbags wh
JOIN handbags h ON wh.id_handbag = h.id_handbag
WHERE h.name IN ('versace')
GROUP BY id_woman
HAVING COUNT(*) = 1
) has_handbag hb ON hb.id_woman = w.id_woman
Upvotes: 1