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