我正在使用MySQL连接。我有两个表,我需要一个连接。第一个表包含所有房地产属性,第二个表包含向该属性添加收藏的用户。现在我想显示登录用户喜欢的图标的所有属性。我在MySQL中编写下面的查询。但是这个查询返回所有记录。左外连接错误导致MYSQL
SELECT a. * , b.property_id AS fav, b.user_id
FROM `property_for_sale` a
LEFT OUTER JOIN `cpo_favourite_property` b ON a.id = b.property_id
WHERE a.property_type = 'Commercial'
UNION
SELECT a. * , b.property_id AS fav, b.user_id
FROM `property_for_rent` a
LEFT OUTER JOIN `cpo_favourite_property` b ON a.id = b.property_id
WHERE a.property_type = 'Commercial'
ORDER BY id DESC
还有一个问题。该查询返回我复制属性如果超过一个用户喜欢的
添加相同的属性下面是我从API获得最终输出时,我使用这个查询
{
"success": "1",
"user": [
{
"id": "1266",
"date": "2016-01-25",
"date_25_days": "2016-02-19",
"date_30_days": "2016-02-24",
"time": "04:47:40 PM",
"user_name": "anil123",
"fileupload": "186_39151.jpg",
"image2": "Null",
"image3": "Null",
"image4": "Null",
"description": "gg",
"fav": "1266",
"user_id": "19"
},
{
"id": "1266",
"date": "2016-01-25",
"date_25_days": "2016-02-19",
"date_30_days": "2016-02-24",
"time": "04:47:40 PM",
"user_name": "anil123",
"fileupload": "186_39151.jpg",
"image2": "Null",
"image3": "Null",
"image4": "Null",
"description": "gg",
"fav": "1266",
"user_id": "480"
},
{
"id": "1144",
"date": "2015-12-07",
"date_25_days": "2016-01-01",
"date_30_days": "2016-01-06",
"time": "05:45:30 PM",
"user_name": "Realtyup Estate Agency",
"fileupload": "464_IMG-20140812-WA0063.jpg",
"image2": "821_IMG-20140812-WA0064.jpg",
"image3": "Null",
"image4": "Null",
"description": "Commercial showroom at Mansa Devi Complex Sector 4 Panchkula.Ground/basement/first/second fully constructed floors.Ample parking.Corner three side open.Well suited for any kind of business establishment",
"fav": null,
"user_id": null
},
{
"id": "625",
"date": "2016-02-01",
"date_25_days": "2016-02-25",
"date_30_days": "2016-03-01",
"time": "02:25:40 AM",
"user_name": "",
"fileupload": "Null",
"image2": "Null",
"image3": "Null",
"image4": "Null",
"description": "VjD7Gu http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com",
"fav": null,
"user_id": null
},
{
"id": "624",
"date": "2016-01-31",
"date_25_days": "2016-02-25",
"date_30_days": "2016-03-01",
"time": "05:44:10 PM",
"user_name": "",
"fileupload": "Null",
"image2": "Null",
"image3": "Null",
"image4": "Null",
"description": "DIMgVX http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com",
"fav": null,
"user_id": null
},
{
"id": "623",
"date": "2016-01-31",
"date_25_days": "2016-02-25",
"date_30_days": "2016-03-01",
"time": "12:59:54 PM",
"user_name": "",
"fileupload": "Null",
"image2": "Null",
"image3": "Null",
"image4": "Null",
"description": "2lx6j8 http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com",
"fav": null,
"user_id": null
}
]
}
你可以在表格/模式中显示样本数据吗? – Stidgeon
在这种情况下,LEFT OUTER join将从左表中返回*所有记录*(仅受WHERE子句限制) - property_for_sale和property_for_rent。它应该是一个INNER连接吗? – user2864740
@Stidgeon请检查我添加结果 –