Php MySQL查询多个列上的多连接两个表

Php MySQL查询多个列上的多连接两个表,php,mysql,sql,database,select,Php,Mysql,Sql,Database,Select,我有一个mysql查询,它从两个表“properties”和“offers”进行交互 “报价”表可以通过使用唯一代码引用特定记录,或通过物业所在的县或地区来匹配物业表中的记录 下面是我的查询示例 SELECT *, ROUND(((3959 * acos(cos(radians(51.1080390)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-4.1610140)) + sin(radians(51.1080390

我有一个mysql查询,它从两个表“properties”和“offers”进行交互

“报价”表可以通过使用唯一代码引用特定记录,或通过物业所在的县或地区来匹配物业表中的记录

下面是我的查询示例

SELECT *, ROUND(((3959 * acos(cos(radians(51.1080390)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-4.1610140)) + sin(radians(51.1080390)) * sin( radians(latitude)))) * 2),0)/2 AS `distance`
FROM `properties` AS prop
LEFT JOIN `offers` ON prop.code = offers.the_property
LEFT JOIN `offers` AS offsCnty ON prop.county = offsCnty.the_county
LEFT JOIN `offers` AS offsRgn ON prop.region = offsRgn.the_region
HAVING distance <= 2.5
ORDER BY `sleeps` ASC, `distance` ASC
LIMIT 0, 10
SELECT*,圆形(((3959*acos(弧度(51.1080390))*cos(弧度(纬度))*cos(弧度(经度)-弧度(-4.1610140))+sin(弧度(51.1080390))*sin(弧度(纬度)))*2),0)/2作为距离`
从'properties'作为prop
在prop.code=offers.the_属性上左键连接'offers'
在prop.county=offsCnty.the_county上左键作为offsCnty加入'offers'
左连接'offers'作为prop.region上的offsRgn=offsRgn.the_区域

使用distance可以连接两个表,并在join子句或where子句中指定额外的连接条件

SELECT *, ROUND(((3959 * acos(cos(radians(51.1080390)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-4.1610140)) + sin(radians(51.1080390)) * sin( radians(latitude)))) * 2),0)/2 AS `distance`
FROM `properties` AS prop
LEFT JOIN `offers` ON prop.code = offers.the_property 
       OR prop.county = offers.the_county 
       OR prop.region = offers.the_region
HAVING distance <= 2.5
ORDER BY `sleeps` ASC, `distance` ASC
LIMIT 0, 10
SELECT*,圆形(((3959*acos(弧度(51.1080390))*cos(弧度(纬度))*cos(弧度(经度)-弧度(-4.1610140))+sin(弧度(51.1080390))*sin(弧度(纬度)))*2),0)/2作为距离`
从'properties'作为prop
在prop.code=offers.the_属性上左键连接'offers'
或prop.county=offers.the_county
或prop.region=offers.the_region

如果以后的报价表中的距离列覆盖了以前的报价表,则需要将它们别名为:

选择*,offers.the _属性从_offers中选择_属性,…

您有三个不同的offers表,它们都具有相同的字段名。问题是MySQL不允许使用相同名称的多个列

最简单的修复方法是在
on
子句中将联接更改为使用

SELECT *, ROUND(((3959 * acos(cos(radians(51.1080390)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-4.1610140)) + sin(radians(51.1080390)) * sin( radians(latitude)))) * 2),0)/2 AS `distance`
FROM `properties` prop LEFT JOIN
     `offers`
      ON prop.code = offers.the_property or
         (prop.county = offsCnty.the_county and offers.the_property is null) or
         prop.region = offsRgn.the_region
HAVING distance <= 2.5
ORDER BY `sleeps` ASC, `distance` ASC
LIMIT 0, 10;
select coalesce(offers.code, offsCnty.code, offsRgn.code) as code

对于报价表中的每一列。

如果您希望最具体的报价优先,我认为您必须这样写:

SELECT prop.*,
       COALESCE(offers.col1, offsCnty.col1, offsRgn.col1) col1,
       COALESCE(offers.col2, offsCnty.col2, offsRgn.col2) col2,
       ...,
       <huge formula> distance
FROM `properties` AS prop
LEFT JOIN `offers` ON prop.code = offers.the_property
LEFT JOIN `offers` AS offsCnty ON prop.county = offsCnty.the_county
LEFT JOIN `offers` AS offsRgn ON prop.region = offsRgn.the_region
HAVING distance <= 2.5
ORDER BY `sleeps` ASC, `distance` ASC
LIMIT 0, 10
选择道具。*,
合并(offers.col1,offsCnty.col1,offsRgn.col1)col1,
合并(offers.col2,offsCnty.col2,offsRgn.col2)col2,
...,
距离
从'properties'作为prop
在prop.code=offers.the_属性上左键连接'offers'
在prop.county=offsCnty.the_county上左键作为offsCnty加入'offers'
左连接'offers'作为prop.region上的offsRgn=offsRgn.the_区域

使用distance
LEFT JOIN
意味着如果没有与
条件上的
匹配的行,则应使用
NULL
填充该行。但只要它匹配3个连接中的一个,您就应该在该组列中获得非空值。我认为它应该是
,而不是
。但我不确定这是否正确,因为这样它就可以匹配
提供的
中的多行,他只需要每个属性一行。感谢您的帮助@Barmar@DevZer0:),关于您关于某一特定地产记录/行是否存在多个报价的观点,通过匹配
该_县
该_地产
,是否可以将计数(*)作为一个动态列来计算报价,然后,我将只显示第一个报价,并显示针对特定属性的更多报价。在MySQL中,获取组的第一行是一个真正的难题,因为它没有
TOP
函数。使用group BY的简单计数(id)现在返回报价的计数。感谢您对多重连接的帮助!Im使用了OR和的组合,这很好:)MySQL允许多个具有相同名称的列,它在结果中使用表别名作为它们的前缀。但是,PHPAPI会丢弃前缀,因此需要使用数字列索引来访问它们。