php - 一次从2个不同的表中获取数据而无需连接php

标签 php mysql sql

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

Table Schema

表:http://gyazo.com/a0839ffc4f5c9cb51458b2b01006c745

我一直在尝试(sql而不是php代码):

从客人中选择名字、姓氏、评分,其中评分 = 5 UNION ALL 从郊区中选择郊区,其中郊区 = '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();
?>

最佳答案

您需要一个简单的邮政编码连接条件和您想要的where条件。

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

关于php - 一次从2个不同的表中获取数据而无需连接php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643392/

相关文章:

php - 循环遍历 PDO 中的数据

MySQL自动增量值设置为零

mysql - SQL:从两列中选择值并显示为一列

sql - 如何将对角线行转换为单行?

php - Apache,如何处理未知的 php 文件,如标准 URL?

php - 查找键的值,其中另一个键的值等于匹配值(关联数组)

sql - MySQL - 选择一个字段有多少个 "Days Old"

php - 链接到新页面时显示MySQL数据

php - 不使用现已弃用的 Google Charts API 的 php 二维码

sql - 运行 SQL Server 2012 Import Export 64 时, 'DbProviderFactories' 部分只能在每个配置文件中出现一次