2015-04-15 33 views
0

因此,我正在創建一個表,我要顯示4個數據字段,3個來自一個表,1個來自另一個表。我曾嘗試使用聯合和其他一些方法,但它似乎不起作用。整個代碼如下。下面給出了我試圖從中獲取數據的2個表的截圖。一次從2個不同的表中獲取數據而不加入php

Table Schema

表:http://gyazo.com/a0839ffc4f5c9cb51458b2b01006c745

我一直想(SQL不是PHP代碼):

SELECT FirstName, LastName, Rating FROM guest WHERE Rating = 5 UNION ALL SELECT Suburb FROM suburb WHERE Suburb = 'Booragoon';

這是PHP代碼:

<?php 
    echo "<table style = 'border; solid 1px black;'>" 
    echo "<tr><th>First Name</th><th>Last Name</th><th>Suburb</th><th>Rating</th></tr>"; 
    class TableRows extends RecursiveInteratorInterator { 
     function __construct($it) { 
      parent::__construct($it, self::LEAVES_ONLY); 
      } 
     function current() { 
      return "<td style='width:160px;border:1px solid black;'>" . parent::curren(). "</td>"; 
     } 
     function beginChildren(){ 
     echo "<tr>"; 
     } 
     function endChildren() { 
     echo "</tr>" . "\n"; 
     } 
    } 
    $Suburb = $_POST['InputSuburb']; 
    $Rating = $_POST['InputRating']; 
    $username = 'root'; 
    $password = ''; 
    $conn = new PDO('mysql:host=localhost;dbname=pizza_shop' $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $conn->prepare//where the command is going 
    $stmt->bindParam('Suburb',$Suburb, PDO::PARAM_STR); 
    $stmt->execute(); 
?> 
+0

您在這兩個查詢中沒有相同的列數。爲什麼你想要它沒有加入? –

+0

@Ameya Deshpande會不會破壞結構?如果不是,我會很樂意使用連接 – csharpnewbie

回答

0

你需要一個簡單的約在郵政編碼和條件你想要的條件。

SELECT FirstName, LastName, Rating,subrup FROM guest inner join suburb 
on guest.postcodefk = suburb.postcode 
WHERE Suburb = 'Booragoon' and Rating = 5; 
相關問題