K
K
Knyashshsh2018-04-29 00:21:10
MySQL
Knyashshsh, 2018-04-29 00:21:10

How to check different strings for the same value?

There is a table for linking posts and tags
5ae4e428eb5d5938470138.png
And I need to pull out, for example, a post_id that has both tag_id 2 and 8.
I can find records with tag_id 2, save its post_id and search for this post_id also tag_id 8, but there can be 5 and it will be +100500 requests.
Or maybe you have other ideas for implementing linking tags to posts and searching through them?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Donskoy, 2018-04-29
@sentike

SELECT post_id
FROM Tags
WHERE tag_id IN (1,2 ,5 , 10, 100)
www.dofactory.com/sql/where-in

C
Codebaker, 2018-04-29
@Codebaker

SELECT DISTINCT post_id FROM table_name WHERE tag_id IN (2, 8, 100500);

D
DTX, 2018-04-29
@DirecTwiX

HAVING sum(1) = &total_tag_coung

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question