Re: Using AND in query

Поиск
Список
Период
Сортировка
Искать
От
David Fetter
Тема
Re: Using AND in query
Дата
Msg-id
20100808220805.GD27785@fetter.org
Ответ на
Re: Using AND in query (David Fetter)
Список
Дерево обсуждения
Using AND in query aravind chandu <avin_friends@yahoo.com>
Re: Using AND in query David Fetter <david@fetter.org>
Re: Using AND in query Raymond O'Donnell <rod@iol.ie>
Re: Using AND in query Thomas Kellerer <spam_eater@gmx.net>
Re: Using AND in query Alban Hertroys <dalroi@solfertje.student.utwente.nl>
Re: Using AND in query Thomas Kellerer <spam_eater@gmx.net>
Re: Using AND in query John R Pierce <pierce@hogranch.com>
Re: Using AND in query David Fetter <david@fetter.org>
Re: Using AND in query David Fetter <david@fetter.org>
On Sun, Aug 08, 2010 at 12:33:40PM -0700, David Fetter wrote:
> On Sun, Aug 08, 2010 at 01:55:19AM -0700, John R Pierce wrote:
> > >The condition
> > >
> > >   and table.item = 'laptop' and table.item = 'Desktop'
> > >
> > >says: I want all rows where the column item has the value
> > >'Laptop' and *at the same time* has the value 'Desktop' Which
> > >clearly cannot be the case (a column can only have a single
> > >value)
> > >
> > >So you need to join all "Laptop" rows to all "Desktop" rows to
> > >get what you want.
> > 
> > why not use OR ?
> > 
> >    ...  AND (table.item = 'laptop' OR table.item='Desktop') ...
> 
> OR doesn't account for duplicates.  Two laptops on the same date
> would cause a false positive.

Thinking this over a little more, it's probably fastest to combine the
approaches, i.e. use both a WHERE clause and a HAVING clause.  For
example:

SELECT TID, "Date"
FROM "table"
WHERE item = ANY(ARRAY['Desktop','Laptop'])
GROUP BY TID, "Date"
HAVING ARRAY['Desktop','Laptop'] <@ array_agg(item);

Cheers,
David
-- 
David Fetter  http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
В списке pgsql-general по дате отправления
От: Glen Eustace
Дата:
От: Scott Frankel
Дата:
Сообщение: Re: MySQL versus Postgres
FAQ