1
我在嘗試使用PHP和MySQL中的不同條件加入多個表時出現以下錯誤。使用PHP和MySQL加入多表時出現SQL錯誤
Error:
Incorrect usage of UNION and ORDER BY
我下面解釋我的查詢。
select b.member_id,b.rest_name,b.city,b.proviance,b.postal,b.address,b.country,b.person,b.mobile,b.url,b.status,b.premium,b.image,b.business_phone_no,b.email,b.multiple_image,b.latitude,b.longitude,b.quadrant,d.member_id,d.day_id,d.cat_id,d.subcat_id,d.comment,d.city,d.special_images,c.cat_id,c.special,sub.subcat_id,sub.subcat_name,sub.status,sl.day_id,sl.member_id,sl.date_from,sl.date_to from db_restaurant_basic as b left join db_restaurant_detail as d on b.member_id=d.member_id left join db_category as c on d.cat_id=c.cat_id left join db_subcategory as sub on d.subcat_id=sub.subcat_id left join db_special_images as sl on d.day_id=sl.day_id and d.member_id=sl.member_id where b.city='2' and d.day_id='3' and c.special='2' and sl.date_from <='2016-10-26' and sl.date_to >= '2016-10-26' and b.status=1 and sub.status=1 and sl.date_from !='' and sl.date_to !='' ORDER BY b.member_id DESC UNION ALL SELECT b.member_id,b.rest_name,b.city,b.proviance,b.postal,b.address,b.country,b.person,b.mobile,b.url,b.status,b.premium,b.image,b.business_phone_no,b.email,b.multiple_image,b.latitude,b.longitude,b.quadrant,d.member_id,d.day_id,d.cat_id,d.subcat_id,d.comment,d.city,d.special_images,c.cat_id,c.special,sub.subcat_id,sub.subcat_name,sub.status,sl.day_id,sl.member_id,sl.date_from,sl.date_to from db_restaurant_basic as b left join db_restaurant_detail as d on b.member_id=d.member_id left join db_category as c on d.cat_id=c.cat_id left join db_subcategory as sub on d.subcat_id=sub.subcat_id left join db_special_images as sl on d.day_id=sl.day_id and d.member_id=sl.member_id where b.city='2' and d.day_id='3' and c.special='2' and b.status=1 and sub.status=1 and sl.date_from ='' and sl.date_to ='' ORDER BY b.member_id DESC
請幫我解決這個錯誤。
我沒有按你,但它拋出這個'再次出現'order clause'錯誤中的未知列'b.member_id'。 – satya
我已更新asnwer添加一個別名cor b.member_id並使用這個別名我命令通過..讓我知道 – scaisEdge
它再次拋出這個'#1052 - 列'member_id'在order子句是不明確的錯誤。 – satya