2017-02-06 198 views
0

residential_projects(表名)如何獲得結果一個屬性上的兩個表

id City Project_name 

1  1  Residential Property 1 

residential_units_details(表名)

id residential_project_id cityId unitType price 
1   1     1  1 BHK 50000 
2   1     1  2 BHK 100000 
3   1     1  3 BHK 150000 

結果,我需要這樣的

Property id =1 
1 BHK = 50000 (price) 
2 BHK = 100000 (price) 
3 BHK = 150000 (price) 

我寫了這樣的查詢,但我沒有得到更正ECT的答案,怎麼辦pleaase我花更多的時間在此,請告訴我解決 我查詢

$unitType = trim($_GET['unitType']); 
    $first_second_tables = "SELECT * FROM residential_projects res_project JOIN residential_units_details res_unit ON res_project.id = res_unit.residential_project_id WHERE City='1'"; 
    if($unitType!='') 
     { 

      $first_second_tables .=" AND (unitType='$unitType')"; 
     } 

$sql=mysql_query($first_second_tables); 
while($res = mysql_fetch_assoc($sql)){ 
    echo "Proprty ID=".$res['id'].'<br>'; 
    echo "Unittype=" . $res['unitType'].'<br>'; 
    echo "Price=" . $res['price'].'<br>'; 
} 

我得到的答案

Proprty ID=1 
Unittype=1 
Price=50000 
Proprty ID=2 
Unittype=2 
Price=100000 
Proprty ID=3 
Unittype=3 
Price=150000 

我覺得這裏印刷在第二表所有提交的數據我不知道如何得到我的要求回答有人幫我

更新代碼var_dump($ res);因爲你只想要unitTypepriceproject_name所以沒有必要使用*爲此,你必須使用left Join輸出

array(3) { 


["unitType"]=> 
    string(1) "1" 
    ["price"]=> 
    string(5) "50000" 
    ["Project_name"]=> 
    string(22) "Residential Property 1" 
} 
array(3) { 
    ["unitType"]=> 
    string(1) "2" 
    ["price"]=> 
    string(6) "100000" 
    ["Project_name"]=> 
    string(22) "Residential Property 1" 
} 
array(3) { 
    ["unitType"]=> 
    string(1) "3" 
    ["price"]=> 
    string(6) "150000" 
    ["Project_name"]=> 
    string(22) "Residential Property 1" 
} 
array(3) { 
    ["unitType"]=> 
    string(1) "4" 
    ["price"]=> 
    string(4) "4343" 
    ["Project_name"]=> 
    string(21) "Residential Project 2" 
} 
array(3) { 
    ["unitType"]=> 
    string(1) "4" 
    ["price"]=> 
    string(4) "5353" 
    ["Project_name"]=> 
    string(21) "Residential Project 2" 
} 
+0

請解釋更多的是因爲你的專欄製造混亂和什麼是'res_project'在'select'查詢。不要使用'mysql *'函數ti's在php 5.5中被棄用,從php 7中刪除,更喜歡'mysqli'或'PDO'。看到這個[鏈接](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – gaurav

+0

我有兩個表,proprty名稱存儲在一個表(residential_projects),和房產價格和單位類型存儲在另一個表(residential_units_details),我有一個屬性和相同的財產我有不同的單位類型(BHK),現在我想顯示結果像1 BHK意味着什麼是價格,和2 BHK意味着什麼是價格 –

+0

我寫的連接查詢連接兩個表 –

回答

0

SQL查詢

SELECT residential_units_details.unitType , residential_units_details.price , residential_projects.project_name 
FROM residential_units_details 
LEFT JOIN residential_projects 
ON residential_units_details.residential_project_id = residential_projects.id 
WHERE City = '1' 

如您發表評論你想poject_name上不重複這樣的標題,你可以嘗試

$str = '' ; /* storing balnk string for matching */ 

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ 
     if(strcmp($row['project_name'],$str) === 0){ /* if the project_name already store it will return 0 */ 

     echo $row['unittype']," = ",$row['price'],"<br>" ; 

     } else { 
     echo $row['project_name'],"<br>"; 
     echo $row['unittype']," = ",$row['price'],"<br>" ; 
     $str = $row['project_name']; /* here we are storing the project_name */ 
     } 
    } 
相關問題