Simple Query?

Поиск
Список
Период
Сортировка
Искать

Simple Query?

От:
Koen Bok <koen@madebysofa.com>
Дата:
I am doing some optimization on our search, but I need some advise...

table: item

id			name
--------------------------------------
1			iPod
2			Zune
3			Walkman

table: search_item

id_search	id_item
--------------------------------------
1			1
1			2
1			3
2			2
2			3
3			1
3			3


Now what I want to have is the items that match with id_search 1 and  
2 and 3. Therefore I use the following SQL query.

SELECT * FROM item WHERE id IN(SELECT id_item FROM search_item WHERE id_search=1 AND id_item IN	(SELECT id_item FROM search_item WHERE id_search=2 AND id_item IN		(SELECT id_item FROM search_item WHERE id_search=3)));

This should only return id_item 3. Would this be the best SQL query  
to get this result? I have the feeling there should be something  
better, but I cannot find it. Anyone has a hint?

Re: Simple Query?

От:
"Rodrigo De León" <rdeleonp@gmail.com>
Дата:
On 9/11/07, Koen Bok  wrote:
> Anyone has a hint?

Another way:

SELECT i.* FROM item i JOIN search_item s1 ON i.ID = s1.id_item      JOIN search_item s2 ON s1.id_item = s2.id_item      JOIN search_item s3 ON s2.id_item = s3.id_itemWHERE s1.id_search = 1  AND s2.id_search = 2  AND s3.id_search = 3

Re: Simple Query?

От:
Richard Broersma Jr <rabroersma@yahoo.com>
Дата:
--- Koen Bok  wrote:
> I am doing some optimization on our search, but I need some advise...
> 
> table: item
> 
> id			name
> --------------------------------------
> 1			iPod
> 2			Zune
> 3			Walkman
> 
> table: search_item
> 
> id_search	id_item
> --------------------------------------
> 1			1
> 1			2
> 1			3
> 2			2
> 2			3
> 3			1
> 3			3
> 
> 
> Now what I want to have is the items that match with id_search 1 and  
> 2 and 3. Therefore I use the following SQL query.

 SELECT id_search   FROM Search_item
GROUP BY id_search HAVING Count( * ) = 3    AND Max( id_item ) = 3    AND Min( id_item ) = 1;

Regards,
Richard Broersma Jr.

Re: Simple Query?

От:
Osvaldo Rosario Kussama <osvaldo_kussama@yahoo.com.br>
Дата:
Koen Bok escreveu:
> I am doing some optimization on our search, but I need some advise...
> 
> table: item
> 
> id            name
> --------------------------------------
> 1            iPod
> 2            Zune
> 3            Walkman
> 
> table: search_item
> 
> id_search    id_item
> --------------------------------------
> 1            1
> 1            2
> 1            3
> 2            2
> 2            3
> 3            1
> 3            3
> 
> 
> Now what I want to have is the items that match with id_search 1 and 2 
> and 3. Therefore I use the following SQL query.
> 
> SELECT * FROM item WHERE id IN
>     (SELECT id_item FROM search_item WHERE id_search=1 AND id_item IN
>         (SELECT id_item FROM search_item WHERE id_search=2 AND id_item IN
>             (SELECT id_item FROM search_item WHERE id_search=3)));
> 
> This should only return id_item 3. Would this be the best SQL query to 
> get this result? I have the feeling there should be something better, 
> but I cannot find it. Anyone has a hint?
> 



SELECT * FROM item WHERE id IN    (SELECT id_item FROM search_item WHERE id_search=1     INTERSECT     SELECT id_item FROM search_item WHERE id_search=2     INTERSECT     SELECT id_item FROM search_item WHERE id_search=3);

Osvaldo

FAQ