简体   繁体   English

无需加入php即可一次从2个不同的表中获取数据

[英]Grabbing Data from 2 different tables at once without a join php

So I'm making a table where I want to show the 4 data fields, 3 from one table and 1 from another table. 所以我要在一个表中显示4个数据字段,其中一个表显示3个数据字段,另一个表显示1个数据字段。 I have tried using a union and a few other methods and it does not seem to work. 我尝试使用联合和其他一些方法,但它似乎不起作用。 The whole code is given below. 整个代码如下。 A screenshot of the 2 tables I'm trying to get data from is also given below. 下面还提供了我尝试从中获取数据的2个表的屏幕快照。

表架构

Tables: http://gyazo.com/a0839ffc4f5c9cb51458b2b01006c745 表格: http//gyazo.com/a0839ffc4f5c9cb51458b2b01006c745

What I've been trying(sql not php code): 我一直在尝试(SQL不是PHP代码):

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

This is the PHP code : 这是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();
?>

you need a simple join condition on postcode and the where condition what you want to. 您需要一个关于邮政编码的简单加入条件以及您想要的where条件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM