简体   繁体   English

PHP和SQL INSERT JOIN 2表有条件错误

[英]PHP and SQL INSERT JOIN 2 tables with criteria error

$rentingsquery = "SELECT * FROM TBL_rentings WHERE personId='$personId'
JOIN TBL_rentings ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId";
$rentingsResult=mysql_query($rentingsquery) or die ("Query to get data from TBL_properties failed: ".mysql_error());

This is the error I get when I execute the code in my web page: 这是我在网页中执行代码时遇到的错误:

在此处输入图片说明

What I'm trying to do is select only the 'rentings' where the personId = $personId , (as the current page is a page for each individual person), and display only those 'rentings'. 我想做的是仅选择personId = $personId的“租金”(因为当前页面是每个人的页面),并且仅显示那些“租金”。 Also in the code which I haven't posted I'm displaying data about the property which is related to that rent, hence why I'm trying to join the renting and properties table with propertyId so that I call the correct property's details off the database. 同样在我未发布的代码中,我正在显示与该租金相关的财产的数据,因此为什么我要尝试使用propertyId将租金和属性表联接在一起,以便我从数据库。

$rentingsquery = "SELECT * FROM TBL_rentings
    JOIN TBL_rentings ON TBL_properties.propertyId = TBL_rentings.propertyId
    JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId
    WHERE personId='$personId'";
    $rentingsResult=mysql_query($rentingsquery) or die ("Query to get data from TBL_properties failed: ".mysql_error());

First of all try this, the where condition has to be after the joins. 首先尝试此操作,连接后必须将where条件设为。 If there are other problems I will edit the answer 如果还有其他问题,我将编辑答案

The next problem is that you are joining the same table, but you are joining it in an external field. 下一个问题是您要联接同一张表,但是要在外部字段中联接它。 A quick fix, if I am supposing correct, is that you wrongly joined with an unwanted table: 如果我认为正确的话,一个快速的解决方法是您错误地加入了不需要的表:

SELECT * FROM TBL_rentings
JOIN TBL_properties ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId
WHERE TBL_rentings.personId='$personId'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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