doula2426 2016-02-02 07:12
浏览 84
已采纳

左外连接错误导致MYSQL

I am working with MySQL joins. I have two tables in which I need a join. First table contains all the real estate properties and second table contains the users who add favorite to that property. Now I want to show all the property with the favorite icon for the user who login. I write the below query in MySQL. But this query returns me all the record.

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

There is one more problem with this. This query return me duplicate property if more than one user add the same property in favorite

Here is the final output I am getting from api when I use this query

{
    "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
        }
    ]
}
  • 写回答

2条回答 默认 最新

  • dongtanzhu5417 2016-02-02 07:27
    关注

    I think you're missing restriction on currently logged user. Add it to the ON clause (:currentUserId placeholder):

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿