How do I join 4 tables on mysql select statement?

后端 未结 1 1099
天命终不由人
天命终不由人 2021-01-29 02:50

I have 4 tables in MySQL to join. Example

$select = $db->query(\"SELECT * 
                        FROM ads 
                       WHERE ad_pic=\'1\' 
               


        
1条回答
  •  南笙
    南笙 (楼主)
    2021-01-29 03:09

    SELECT *
    FROM ads AS a
    LEFT JOIN images AS i ON i.img_ads_id = a.ad_id AND i.img_status = 1
    LEFT JOIN users AS u ON u.id = a.ad_userid AND u.user_status = 1
    LEFT JOIN category AS c ON c.cat_slug = a.ad_category
    WHERE a.ad_pic = 1
    AND a.ad_status = 1
    ORDER BY a.ad_id DESC
    LIMIT 0,4
    

    If an ad must have an image, user, or category, you may use a JOIN instead of a LEFT JOIN. If an ad can have more than one image, user, or category, you will need a more complex query.

    0 讨论(0)
提交回复
热议问题