List:General Discussion« Previous MessageNext Message »
From:Suresh Kuna Date:October 16 2011 12:06pm
Subject:Re: [MYSQL] INTERSECT, MINUS
View as plain text  
Hi,

EXISTS function provides a simple way to find intersection between tables
(INTERSECT operator from relational model).

If we have table1 and table2, both having id and value columns, the
intersection could be calculated like this:

SELECT * FROM table1 WHERE EXISTS(SELECT * FROM table2 WHERE table1.id=
table2.id AND table1.value=table2.value)

For more details on intersect and minus, check this blog -
http://www.bitbybit.dk/carsten/blog/?p=71

Thanks
Suresh Kuna

On Sun, Oct 16, 2011 at 5:12 PM, Grega Leskovšek <legrega@stripped> wrote:

> WHat is wring with the following three sentences?
>
> SELECT p.name, p.gender, e.pizza FROM Person p, Eats e WHERE p.name =
> e.name AND p.gender = 'female' AND (e.pizza = 'mushroom')
> INTERSECT
> SELECT p.name, p.gender, e.pizza FROM Person p, Eats e WHERE p.name =
> e.name AND p.gender = 'female' AND (e.pizza = 'pepperoni');
>
>
> SELECT * FROM Person MINUS SELECT * FROM Person WHERE name='Amy';
>
>
> mysql> SELECT name FROM Person MINUS SELECT name FROM Person WHERE
> Person.age <
> 18;
>
> I've tried my first time sets and am not sure where is the problem,
> I've tried to google but when translating to my db it just doesn't
> work,
> You can download the create db sql here:
> http://s3.amazonaws.com/dbclass-resources/docs/pizza.sql
>
> ♥♥♥ When the sun rises I receive and when it sets I forgive!
> ♥♥♥
> ˜♥ -> http://moj.skavt.net/gleskovs/ <- ♥ Always, Grega
> Leskovšek
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=1
>
>


-- 
Thanks
Suresh Kuna
MySQL DBA

Thread
[MYSQL] INTERSECT, MINUSGrega LeskovÅ¡ek16 Oct
  • Re: [MYSQL] INTERSECT, MINUSSuresh Kuna16 Oct