简体   繁体   English

SQL查询协助-左联接意外结果

[英]SQL Query Assistance - Left join unexpected result

I have these 2 queries: 我有以下两个查询:

$sql = "SELECT *
FROM ultrait_wpl_properties
LEFT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id  ";

$sql2 = "SELECT * 
FROM ultrait_wpl_properties, ultrait_wpl_property_types
WHERE  ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id";

For some odd reason when the IDs are output some are duplicated? 由于某些奇怪的原因,当输出ID时有些重复? By my reseaning these queries should get everything from the table in the first part and join the second table based on the WHERE condition. 通过我的重申,这些查询应该从第一部分的表中获取所有内容,并根据WHERE条件连接第二个表。

<property><id>13</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>7</id></property>

This may be slightly unclear but for some reason I'm getting duplicate IDs, all i want really is to be able to access the property type which links to the ID in the second table. 这可能还不太清楚,但是由于某种原因,我得到了重复的ID,我真正想要的只是能够访问链接到第二张表中ID的属性类型。

I have tested both queries in phpMyAdmin and they yeild the desired result, however when I use the queries in my php script they return unexpected results. 我已经在phpMyAdmin中测试了这两个查询,它们获得了所需的结果,但是当我在php脚本中使用查询时,它们返回了意外的结果。

You are getting a Cartesian result in the case the ultrait_wpl_property_types table has multiple records for a single property. 在ultrait_wpl_property_types表具有单个属性的多个记录的情况下,您将得到笛卡尔结果。 Such as a property type could be Type A, Type B, Type C which might be descriptive "types". 例如,属性类型可以是Type A,Type B,Type C,它们可能是描述性的“类型”。 So a single property would be accounted for each entry. 因此,将为每个条目考虑一个属性。

You might just need to do SELECT DISTINCT, or GROUP BY ultrait_wpl_properties.id to make sure only one record per ID, but with generic "Select * ", I would first try with GROUP BY. 您可能只需要执行SELECT DISTINCT或GROUP BY ultrait_wpl_properties.id,以确保每个ID仅记录一条,但是对于通用的“ Select *”,我首先尝试使用GROUP BY。

You are getting one row for each row in table ultrait_wpl_properties. 您在表ultrait_wpl_properties中的每一行得到一行。 What else do you expect? 您还期望什么? If it is just one record per type, then you would have to re-write your query accordingly. 如果每种类型只有一条记录,则必须相应地重新编写查询。 You select * from both tables. 您从两个表中选择*。 But is it only the type ID you need? 但这仅仅是您需要的类型ID吗? Then why join the tables at all? 那为什么要完全加入表格呢?

Get all type IDs: 获取所有类型ID:

select id from ultrait_wpl_property_types;

Get all type IDs in table ultrait_wpl_properties: 获取表ultrait_wpl_properties中的所有类型ID:

select distinct property_type from ultrait_wpl_properties;

Get all type data for types in ultrait_wpl_properties: 获取ultrait_wpl_properties中类型的所有类型数据:

select * from ultrait_wpl_property_types
where id in (select property_type from ultrait_wpl_properties);

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

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