SearchSearch   

mysql intersect help

 
   Webmaster Forums (Home) -> MySQL RSS
Next:  How can i dump (mysqldump) with skip query loggin..  
Author Message
prathamesh.deshpande

External


Since: May 09, 2007
Posts: 2



(Msg. 1) Posted: Wed May 09, 2007 9:50 pm
Post subject: mysql intersect help
Archived from groups: mailing>database>mysql (more info?)

Hi,
I am facing problem while excuting a mysql query. My problem is as
follows-
I have two tables object_1 and object_2.
The structure of object_1 is

object_id name address
------------ ---------- ------------
1 abc xyz
2 pqr xyz
3 lmn zzz

The structure of object_2 is

object_id tag_id
-------------- ----------
1 2
1 3
2 2

object_id is the foreign key in the table object_2

I want to find all the rows from object_1 whose tag_id is 2 and 3.
ie my result row will be

object_id name address
------------ ---------- ------------
1 abc xyz

So technically what i want to do is an intersect.But since mysql
doesnt support intersect i am not able to do it
Can anyone suggest how to go about it?
Thanks
Back to top
lark

External


Since: May 07, 2007
Posts: 17



(Msg. 2) Posted: Thu May 10, 2007 10:24 am
Post subject: Re: mysql intersect help
Archived from groups: per prev. post (more info?)

prathamesh.deshpande DeleteThis @gmail.com wrote:
> Hi,
> I am facing problem while excuting a mysql query. My problem is as
> follows-
> I have two tables object_1 and object_2.
> The structure of object_1 is
>
> object_id name address
> ------------ ---------- ------------
> 1 abc xyz
> 2 pqr xyz
> 3 lmn zzz
>
> The structure of object_2 is
>
> object_id tag_id
> -------------- ----------
> 1 2
> 1 3
> 2 2
>
> object_id is the foreign key in the table object_2
>
> I want to find all the rows from object_1 whose tag_id is 2 and 3.
> ie my result row will be
>
> object_id name address
> ------------ ---------- ------------
> 1 abc xyz
>
> So technically what i want to do is an intersect.But since mysql
> doesnt support intersect i am not able to do it
> Can anyone suggest how to go about it?
> Thanks
>
have you tried something like this:

select
object_1.object_id,
object_1.name,
object_1.address
from object_1
where object_1.object_id in (select object_id from object_2 where tag_id
in (2,3) )




--
lark -- hamzee DeleteThis @sbcdeglobalspam.net
To reply to me directly, delete "despam".
Back to top
prathamesh.deshpande

External


Since: May 09, 2007
Posts: 2



(Msg. 3) Posted: Sun May 13, 2007 10:44 pm
Post subject: Re: mysql intersect help
Archived from groups: per prev. post (more info?)

Hi,
Yes i tried it. It is not working. This is because in essentially
behaves like or
Back to top
strawberry

External


Since: Apr 20, 2007
Posts: 21



(Msg. 4) Posted: Mon May 14, 2007 10:03 am
Post subject: Re: mysql intersect help
Archived from groups: per prev. post (more info?)

On May 14, 6:44 am, prathamesh.deshpa....DeleteThis@gmail.com wrote:
> Hi,
> Yes i tried it. It is not working. This is because in essentially
> behaves like or

Guys, guys

Stop using this subselect rubbish and read up on JOINS instead

SELECT t1. *
FROM object_1 t1
JOIN object_2 t2a ON t1.object_id = t2a.object_id
AND t2a.tag_id =2
INNER JOIN object_2 t2b ON t1.object_id = t2b.object_id
AND t2b.tag_id =3
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> MySQL
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum