简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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