我正在使用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請檢查我添加結果 –