[英]What is the effective query to retrieve related information from related MySQL tables, when there can be search in both tables
[英]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.