簡體   English   中英

左外連接錯誤導致MYSQL

[英]Left outer join wrong result in 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
        }
    ]
}

我認為您缺少對當前登錄用戶的限制。 將其添加到ON子句( :currentUserId占位符):

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 AND b.user_id = :currentUserId
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 AND b.user_id = :currentUserId
WHERE a.property_type = 'Commercial'
ORDER BY id DESC

如果您想同時顯示兩個用戶,請使用以下查詢-

SELECT a. * , b.property_id AS fav, group_concat(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'
group by a.id
UNION
SELECT a. * , b.property_id AS fav, group_concat(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'
group by a.id
ORDER BY id DESC

注意:如果您不想顯示多個用逗號分隔的用戶,則不要使用group_concat函數,那么您的查詢只會給您第一個用戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM