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

时间:2014-03-11 06:01:49

标签: php mysql

我正在尝试从同一个数据库(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);
?>

1 个答案:

答案 0 :(得分:0)

尝试这个

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