在没有连接php的情况下一次从2个不同的表中获取数据

时间:2015-04-15 06:50:23

标签: php mysql sql

所以我正在制作一个表格,我想要显示4个数据字段,3个来自一个表格,1个来自另一个表格。我尝试过使用union和其他一些方法,但似乎没有用。整个代码如下。我试图从中获取数据的2个表的屏幕截图也在下面给出。

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();
?>

1 个答案:

答案 0 :(得分:0)

你需要在邮政编码上简单的连接条件以及你想要的where条件。

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