0

I'll start off with my table:

id    |   name    |   label_id  | package_id
 1        Fred         23742        1056
 2        Fred         23742        1351
 3        Jack         43743        1057
 4        Jack         43743        1352
 5        Bob          13523        1056
 6        Anna         58232        1056
 7        Anna         58232        1057

So each person has their own label id but shared package_ids. I just want the all the people that could have package ids of (1056 OR 1057) AND (1351 OR 1352 OR 1353)

 1        Fred         23742        1056
 2        Fred         23742        1351

 3        Jack         43743        1057
 4        Jack         43743        1352

 6        Anna         58232        1056
 7        Anna         58232        1353

but exclude people that have a single row like bob

 5        Bob          13523        1056

I'll try to elaborate more if needed because its difficult for me to explain.

1
  • In your table...the 2nd pack id for Anna is 1057 but 1353 in your results... Commented Aug 6, 2015 at 18:47

2 Answers 2

1

I believe you are looking for something like this. This will select all rows where the package_id is in the numbers you listed, and excludes people that only have one row in the table.

select * from <table-name> 
where package_id in (1056, 1057, 1351, 1352, 1343)
and name in (
    select name from <table-name>
    group by name
    having count(*) > 1
);
Sign up to request clarification or add additional context in comments.

2 Comments

This will not address the part saying "I just want the all the people that could have package ids of (1056 OR 1057) AND (1351 OR 1352 OR 1353)".
Ah, I misunderstood the question.
0

SQL Fiddle

MySQL 5.6 Schema Setup:

CREATE TABLE mytable
    (`id` int, `name` varchar(4), `label_id` int, `package_id` int)
;

INSERT INTO mytable
    (`id`, `name`, `label_id`, `package_id`)
VALUES
    (1, 'Fred', 23742, 1056),
    (2, 'Fred', 23742, 1351),
    (3, 'Jack', 43743, 1057),
    (4, 'Jack', 43743, 1352),
    (5, 'Bob', 13523, 1056),
    (6, 'Anna', 58232, 1056),
    (7, 'Anna', 58232, 1353)
;

Query 1:

select *
from mytable
where name in (
    select name
    from (
        select distinct name 
        from mytable
        where package_id in (1056, 1057)
        union all
        select distinct name 
        from mytable
        where package_id in (1351, 1352, 1353)
   ) as result
   group by name
   having count(*) > 1)

Results:

| id | name | label_id | package_id |
|----|------|----------|------------|
|  1 | Fred |    23742 |       1056 |
|  2 | Fred |    23742 |       1351 |
|  3 | Jack |    43743 |       1057 |
|  4 | Jack |    43743 |       1352 |
|  6 | Anna |    58232 |       1056 |
|  7 | Anna |    58232 |       1353 |

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.