2011-08-28 6 views
0

여기에 하위 쿼리로 할 일이 있지만 할 수 없습니다.테이블 1의 ID를 가진 테이블 2의 행을 가져 오지 않습니다.

CountryId Country ISO2 ISO3 

표 2 :

id  noof_country state 

I 나는 표 1
표에서 열 값을 다른 테이블에서 결과 더에 표시되지하기 위해 추가 필드가있는 테이블에서 결과를 원하는 카운트 필드로 표 1에

편집
내 실제 테이블을 계산 noof_country PIN이 필요

재하는 것은 표 1 :이 단계에서 CountryId 나라 ISO2 상태

내가 수행 한 질의 : 광고 ID의 job_country 상태 DAYS_LEFT

테이블이 삭제

여기
$sql_map = "select distinct c.ISO2, c.Country, a.job_country 
    from rec_countries c, rec_advert a 
    where c.status = 1 
    and DATE(a.modified_date) >= CURDATE() - INTERVAL 30 DAY 
    and c.ISO2 <> '--' 
    and c.ISO2 <> '' 
    and c.CountryId = a.job_country 
    and a.status = 1 
    and a.`delete` = 0 
    and a.days_left >0 
    "; 
$res = mysql_query($sql_map); 

while($row = mysql_fetch_array($res)){ 
    $jobs_no = count($row['job_country']); 
    $sql_job = "SELECT COUNT(job_country) AS jobs_no 
    FROM rec_advert 
    WHERE job_country = ".$row['job_country']." 
    and status = 1 
    and `delete` = 0 
    and days_left >0"; 
    $resjob=mysql_query($sql_job); 
    $rowjob = mysql_fetch_array($resjob); 

    //here jobs_no is the count of total rows 
} 

내가하고 싶은 부질의와. 내가 질문 권리를 읽는다면

답변

2

이 작동합니다 :

SELECT 
    CountryId, 
    Country, 
    ISO2, 
    ISO3, 
    (
     SELECT COUNT(DISTINCT noof_country) 
     FROM table2 
     WHERE table2.id = table1.CountryId 
    ) AS noof_country_count 
FROM table1 

그것은 table1에있는 열 귀하의 질문에 즉시 명확하지 것은 table2의 컬럼에 외래 키입니다 ... 또는 그들은 심지어 경우 그런 식으로. 이 쿼리가 작동하지 않으면 스키마를 명확히하십시오.


업데이트 된 정보를 바탕으로,이 시도 :

select distinct c.ISO2, c.Country, a.job_country, 
    (
     select COUNT(a2.job_country) 
     from rec_advert a2 
     where a2.job_country = a.job_country 
     and a2.status = 1 
     and a2.`delete` = 0 
     and a2.days_left >0 
    ) as jobs_no 
    from rec_countries c, rec_advert a 
    where c.status = 1 
    and DATE(a.modified_date) >= CURDATE() - INTERVAL 30 DAY 
    and c.ISO2 <> '--' 
    and c.ISO2 <> '' 
    and c.CountryId = a.job_country 
    and a.status = 1 
    and a.`delete` = 0 
    and a.days_left >0 
+0

내가 위에서 약간의 편집이있다. 그것에 조언을 좀 해줄 수 있니? – kamal

+0

답변이 업데이트되었습니다. – cdhowie

관련 문제