MySQL select all records that match the same others records in another table
up vote
0
down vote
favorite
I have two tables:
Table _models with fields name and model_id
And
Table _tags with fields tag_name, tags_id and model_id
In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.
How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?
For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench
mysql join
add a comment |
up vote
0
down vote
favorite
I have two tables:
Table _models with fields name and model_id
And
Table _tags with fields tag_name, tags_id and model_id
In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.
How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?
For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench
mysql join
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have two tables:
Table _models with fields name and model_id
And
Table _tags with fields tag_name, tags_id and model_id
In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.
How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?
For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench
mysql join
I have two tables:
Table _models with fields name and model_id
And
Table _tags with fields tag_name, tags_id and model_id
In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.
How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?
For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench
mysql join
mysql join
asked Nov 21 at 21:29
fboc
31
31
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.
SELECT m.model_id, m.name
FROM models m
INNER JOIN tags t
ON m.model_id = t.model_id
WHERE t.tag_name IN ('#jacket', '#trench')
GROUP BY m.model_id, m.name
HAVING COUNT(DISTINCT t.tag_name) = 2;
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
add a comment |
up vote
0
down vote
Slightly different way to do the same:
SELECT
m.model_id
, m.name
FROM
_models m
WHERE
2 = (
SELECT
COUNT(*)
FROM
_tags t
WHERE
m.model_id = t.model_id
AND t.tag_name IN ('#jacket', '#trench')
)
Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)
Note 2: Do not forget to add index (tag_name, model_id) on table _tags
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.
SELECT m.model_id, m.name
FROM models m
INNER JOIN tags t
ON m.model_id = t.model_id
WHERE t.tag_name IN ('#jacket', '#trench')
GROUP BY m.model_id, m.name
HAVING COUNT(DISTINCT t.tag_name) = 2;
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
add a comment |
up vote
0
down vote
accepted
For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.
SELECT m.model_id, m.name
FROM models m
INNER JOIN tags t
ON m.model_id = t.model_id
WHERE t.tag_name IN ('#jacket', '#trench')
GROUP BY m.model_id, m.name
HAVING COUNT(DISTINCT t.tag_name) = 2;
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.
SELECT m.model_id, m.name
FROM models m
INNER JOIN tags t
ON m.model_id = t.model_id
WHERE t.tag_name IN ('#jacket', '#trench')
GROUP BY m.model_id, m.name
HAVING COUNT(DISTINCT t.tag_name) = 2;
For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.
SELECT m.model_id, m.name
FROM models m
INNER JOIN tags t
ON m.model_id = t.model_id
WHERE t.tag_name IN ('#jacket', '#trench')
GROUP BY m.model_id, m.name
HAVING COUNT(DISTINCT t.tag_name) = 2;
answered Nov 21 at 21:42
Joe Stefanelli
109k13190204
109k13190204
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
add a comment |
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
This is exactly the solution of my problem, thank you Joe!
– fboc
Nov 21 at 22:50
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
@fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 22 at 5:02
add a comment |
up vote
0
down vote
Slightly different way to do the same:
SELECT
m.model_id
, m.name
FROM
_models m
WHERE
2 = (
SELECT
COUNT(*)
FROM
_tags t
WHERE
m.model_id = t.model_id
AND t.tag_name IN ('#jacket', '#trench')
)
Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)
Note 2: Do not forget to add index (tag_name, model_id) on table _tags
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
add a comment |
up vote
0
down vote
Slightly different way to do the same:
SELECT
m.model_id
, m.name
FROM
_models m
WHERE
2 = (
SELECT
COUNT(*)
FROM
_tags t
WHERE
m.model_id = t.model_id
AND t.tag_name IN ('#jacket', '#trench')
)
Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)
Note 2: Do not forget to add index (tag_name, model_id) on table _tags
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
add a comment |
up vote
0
down vote
up vote
0
down vote
Slightly different way to do the same:
SELECT
m.model_id
, m.name
FROM
_models m
WHERE
2 = (
SELECT
COUNT(*)
FROM
_tags t
WHERE
m.model_id = t.model_id
AND t.tag_name IN ('#jacket', '#trench')
)
Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)
Note 2: Do not forget to add index (tag_name, model_id) on table _tags
Slightly different way to do the same:
SELECT
m.model_id
, m.name
FROM
_models m
WHERE
2 = (
SELECT
COUNT(*)
FROM
_tags t
WHERE
m.model_id = t.model_id
AND t.tag_name IN ('#jacket', '#trench')
)
Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)
Note 2: Do not forget to add index (tag_name, model_id) on table _tags
answered Nov 22 at 0:52
fifonik
26515
26515
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
add a comment |
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
– fboc
Nov 22 at 7:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420717%2fmysql-select-all-records-that-match-the-same-others-records-in-another-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown