Cheers to all,
I have a table with these data
service_id-----------entity_id
---5---------|--------1------
---6---------|--------1------
---5---------|--------2------
---6---------|--------3------
What I need to select:
-----------------------
All entity_id's with service_id's equal to 5 and 6.
(or all entities that have both service 5 and service 6)
In this case this is entity_id=1 (but not 2 and 3).
Is there a way to do this?
And how would I do it?
I've tried to search and read up on it, but frankly I have not found
anything.
I have a strong feeling that this cannot be done, unless you go into
code and have logic there.
I'd also gladly do it with temp table, but that too didnt inspire a
solution from me.
The only way I thought of would be with 'GROUP_CONCAT', but that is
cheesy and as I use mysql this is not supported (yet).
Please Help,
Thanks a lot
Damjan
PS if for some reason you need sql for table and data:
INTRO:
-------
I have table mapper:
CREATE TABLE mapper (
service_id integer NOT NULL,
entity_id integer NOT NULL
)
GO
with data:
INSERT INTO mapper (service_id, entity_id) VALUES (5, 1);
go
INSERT INTO mapper (service_id, entity_id) VALUES (6, 1);
go
INSERT INTO mapper (service_id, entity_id) VALUES (5, 2);
go
INSERT INTO mapper (service_id, entity_id) VALUES (6, 3);
go
to view data:
SELECT
m.service_id,
m.entity_id
FROM mapper AS m
ORDER BY m.entity_id
Gene Wirchenko - 30 Aug 2004 16:59 GMT
>I have a table with these data
>
[quoted text clipped - 3 lines]
>---5---------|--------2------
>---6---------|--------3------
>What I need to select:
>-----------------------
[quoted text clipped - 5 lines]
>Is there a way to do this?
>And how would I do it?
Salt with semicolons as needed:
select entity_id,service_id from mapper
group by entity_id,service_id where service_id in (5,6)
into cursor work
select entity_id from work
group by entity_id having count(*)=2
The first statement generates a cursor with one record for each
entity_id-service_id combination where service_id is 5 or 6.
The second statement reports on all service_id having two rows.
The only way for this to happen is if there are both a 5 row and a 6
row for that service_id.
If your SQL DBMS supports queries in the from clause, you could
combine the two statements.
[snip]
Sincerely,
Gene Wirchenko
Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
-P- - 30 Aug 2004 17:44 GMT
> Cheers to all,
>
[quoted text clipped - 21 lines]
> I have a strong feeling that this cannot be done, unless you go into
> code and have logic there.
Your strong feeling is unjustified...
Select entity_id
from Mapper M1
where
M1.service_id = 5 and
exists( select 1
from Mapper M2
where M2.entity_id = M1.entity_id and
M2.service_id = 6 ) ;

Signature
Paul Horan
Sr. Architect
VCI Springfield, MA
www.vcisolutions.com
Laconic2 - 30 Aug 2004 21:41 GMT
> Cheers to all,
>
[quoted text clipped - 60 lines]
> FROM mapper AS m
> ORDER BY m.entity_id
select m1.entity_id
from mapper as m1,
mapper as m2
where
m1.entity_id = m2.entity_id
and m1. service_id = 5
and m2.service_id = 6;
damjanu@hotmail.com - 31 Aug 2004 10:28 GMT
Thanks guys for help and your time