Re: A MySQL Question
| From: | German Geek | Date: | Mon, 08 Dec 2008 06:13:09 +0000 |
| Subject: | Re: A MySQL Question | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-284304@lists.php.net to get a copy of this message | ||
On Mon, Dec 8, 2008 at 7:06 PM, Chris <dmagick@gmail.com> wrote:
>
> Right....
>>
>> So, how are these different:
>>
>> SELECT * FROM t1 WHERE id EXISTS (SELECT id FROM t2)
>>
>
> If there are *any* results for the subselect, the exists returns true.
>
> It's the equivalent of:
>
> select * from t1 where id is true;
>
> ie
>
> select * from t1;
>
> If there are no results for the subselect, the exists returns false, ie:
>
> select * from t1 where false;
>
> which will return nothing.
>
> to
>> SELECT * FROM t1 WHERE id IN (SELECT id FROM t2)
>>
>
> this returns specific id's that match.
>
> According to my understanding of the documentation, these would have the
>> same result.
>>
>
> No, they aren't.
>
> create table t1(id int, name varchar(5));
>
> insert into t1(id, name) values (1, 'one');
> insert into t1(id, name) values (2, 'two');
> insert into t1(id, name) values (3, 'three');
> insert into t1(id, name) values (4, 'four');
> insert into t1(id, name) values (5, 'five');
>
> create table t2(id int, other_name varchar(5));
>
> insert into t2(id, other_name) values (1, 'one');
> insert into t2(id, other_name) values (2, 'two');
>
> this returns everything from t1:
> SELECT * FROM t1 WHERE EXISTS (SELECT id FROM t2);
>
> this returns 2 rows that match:
> SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);
Oh OK. Thanks for clearing that up.
>
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>
>
--
Tim-Hinnerk Heuer
http://www.ihostnz.com -- Web Design, Hosting and free Linux
Support