2011-02-18 2 views
2

먼저 데이터베이스 예 : I는 다음과 아이디의 선택 하나 개의 쿼리로 원하는선택 경우 값 현재

id, product_id, cat, name, value 
-------------------------------- 
1,1,Algemeen,Processor,2 Ghz 
2,1,Algemeen,Geheugen,4 GB 

3,2,Algemeen,Processor,3 Ghz 
4,2,Algemeen,Geheugen,4 GB 

5,3,Beeldscherm,Inch,22" 
6,3,Beeldscherm,Kleur,Zwart 
7,3,Algemeen,Geheugen,3 GB 
8,3,Algemeen,Processor,3 Ghz 

: 1,2,3,4,7,8

때문에 고양이 = algemeen 및이 제품의 이름 = 프로세서. ID 5,6은 제품 3에서만 제공됩니다.

따라서 모든 제품 (product_id)이 제공하는 항목 (고양이 및 이름)을 선택해야합니다.

데이터베이스에는 다른 고양이, 이름 및 가치가 많이 포함 된 80,000 개의 항목이 포함되어 있습니다.

하나의 쿼리로 가능합니까 아니면 일부 PHP가 필요합니까? 어떻게해야합니까?

나쁜 영어에 대한 사과.

답변

0
<?php 
$query = mysql_query(" 
select distinct p.cat, p.name, p.value 
from producten_specs p 
inner join ( 
select cat, `name`, count(*) as cnt 
from producten_specs 
where product_id in (".mysql_real_escape_string($product_ids).") 
group by cat, `name` 
having cnt = ".mysql_real_escape_string($product_ids_aantal)." 
) c on p.cat = c.cat and p.`name` = c.`name` 
where p.product_id in (".mysql_real_escape_string($product_ids).") 
ORDER BY cat,name,ABS(value) ASC 
"); 
?> 
0

나는 모든 product_ids에 (cat, name)의 조합이있는 모든 레코드를 표시한다고 생각하십니까?

select t.id, t.product_id, t.cat, t.name, t.value 
from (
    select cat, name 
    from tbl 
    group by cat, name 
    having count(product_id) = count(distinct product_id) 
) p 
join tbl t on t.cat = p.cat and t.name = p.name 
0

설명하기가 어렵습니다. 쿼리의 결과를 볼 때 그것이 의미하는 바를 안다.

SELECT DISTINCT cat, name, value 
    FROM producten_specs 
    WHERE product_id IN (1,2,3) 
    ORDER BY cat,name,ABS(value) ASC 

당신과 함께이 쿼리를 결합 할 수 있습니다 :

는 내가 모든 항목을 표시하기 위해 사용하고 쿼리를 작성하는 것을 잊었다.

0

문제에 대한 설명을 바탕으로 cat 값이 'Algemeen'이거나 값이 product_id 인 레코드가 '프로세서'가되는 SQL 쿼리를 원합니다. 이 경우 다음 쿼리를 사용합니다.

SELECT * 
    FROM producten_specs AS p 
    WHERE p.cat = "Algemeen" OR p.product_id = "Processor" 
    ORDER BY p.id 

이 질문은 1 년 이상 된 것으로 확인되었습니다. 이 사이트에서 원하는 답변을 찾은 경우이를 수락해야합니다. 질문을 닫지 않은 경우 닫으십시오.