简体   繁体   English

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

[英]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.

相关问题 当可以在两个表中进行搜索时,从相关的MySQL表中检索相关信息的有效查询是什么? - What is the effective query to retrieve related information from related MySQL tables, when there can be search in both tables PHP / MySQL选择查询以浏览数据库中的四个相关表 - PHP/MySQL select query to go through four related tables in database 使用PHP OOP从数据库中的mysql检索信息 - retrieve information from a mysql from database using PHP OOP 在PHP / MySql中通过单个查询从多个表中获取“相关”数据? - get 'related' data from several tables with a single query in PHP/MySql? 使用PHP表单中的2个字段来从MySQL数据库中检索信息 - Issue using 2 fields in PHP form to retrieve information from a MySQL Database php mysql 从数据库中的三个表中选择信息 - php mysql select information from three tables from the database 无法将信息从PHP表单发送到多个mysql数据库表 - Can not send information from PHP form to multiple mysql database tables 检索多个复选框并在表中显示数据库中的其他相关信息 - Retrieve multiple checkbox and display other related information from database in a table 将图像和信息存储到数据库中,并使用MySql Query检索以使用PHP显示 - Storing image and information into database and retrieve using MySql Query to display using PHP 无法使用PHP从数据库检索信息 - Cant retrieve information from database using php
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM