繁体   English   中英

mySQL查询可从1个数据库PHP的2个表中检索相关信息

[英]mySQL query to retrieve related information from 2 tables on 1 database PHP

我正在尝试从同一数据库(mySQL)的2个表中检索数据。 我尝试使用INNER JOIN,但是我已经意识到用于SELECT条目的WHERE在两个表上都不相同,即。 属性上的id与报告上的id不同,因此我无法告诉查询从第二个表中获取准确的信息(我认为是对的)

无论如何。 下面是我的查询。 有人可以告诉我获得正确结果的最佳方法,我需要正确地执行此操作并填充所有这些字段?

干杯

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

试试这个

<?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.

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