[英]mySQL query to retrieve related information from 2 tables on 1 database PHP
I am trying to retrieve data from 2 tables in the same database (mySQL). 我正在尝试从同一数据库(mySQL)的2个表中检索数据。 I have tried to use an INNER JOIN but I have realised that the WHERE used to SELECT entries is not the same on both tables ie.
我尝试使用INNER JOIN,但是我已经意识到用于SELECT条目的WHERE在两个表上都不相同,即。 id on properties is not the same as id on reports so I have no way of telling the query to get accurate info from the second table ( i think that's right)
属性上的id与报告上的id不同,因此我无法告诉查询从第二个表中获取准确的信息(我认为是对的)
anyway. 无论如何。 below is my query.
下面是我的查询。 Can someone please tell me the best way to achieve the results i need to do this properly and have all of those fields populate?
有人可以告诉我获得正确结果的最佳方法,我需要正确地执行此操作并填充所有这些字段?
cheers 干杯
<?php session_start();
header('Content-type: application/json');
require_once('DbConnector.php');
$connector = new DbConnector();
$customerid = '125';
$sql=( "SELECT DISTINCT reports.id, properties.title, reports.title, reports.date, reports.link FROM reports JOIN properties ON reports.visible = properties.customer WHERE properties.customer ='".$customerid."'" )or die( mysql_error("FAIL!!") );
$result = mysql_query($sql);
while($data = mysql_fetch_assoc($result)) {
$output[] = $data;
}
echo json_encode($output);
?>
try this one 试试这个
<?php session_start();
header('Content-type: application/json');
require_once('DbConnector.php');
$connector = new DbConnector();
$customerid = '125';
$sql=( "SELECT DISTINCT reports.id, properties.title, reports.title, reports.date, reports.link FROM reports JOIN properties ON reports.visible = properties.visible WHERE properties.customer = '$customerid' " )or die( mysql_error("FAIL!!") );
$result = mysql_query($sql);
while($data = mysql_fetch_assoc($result)) {
$output[] = $data;
}
echo json_encode($output);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.