1

I need to create a MySQL query which searches for values, but I need the results to return which have the same content_id number (which is unknown until searched for).

Here's my table:

content_id      item_id         tag_id
1               4               2
1               4               3
1               4               4
1               4               5
2               5               7
2               5               8
3               6               8
3               6               9

And currently here's my query:

SELECT item_id FROM table WHERE tag_id IN (7,8)

This is returning:

Array
(
    [0] => stdClass Object
        (
            [item_id] => 5
        )

    [1] => stdClass Object
        (
            [item_id] => 5
        )

    [2] => stdClass Object
        (
            [item_id] => 6
        )

)

But I need it to return (because of content_id = 2 in this instance):

Array
(
    [0] => stdClass Object
        (
            [item_id] => 5
        )

)

I hope this makes sense..

1
  • 1
    then just add an and clause and group Commented Nov 30, 2016 at 1:27

1 Answer 1

2

You need to gather the content_id into groups, then check which group has count more than 1.

SELECT item_id FROM table group by content_id having count(content_id) > 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.