2016-10-22 4 views
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 

このエラーを解決するのを手伝ってください。

答えて

1

あなたは、それぞれが
を選択して、別のテーブルに同じ名前の列に対して適切なエイリアスを使用していない内側のクエリの端部のみで順位を割り当てる必要があり

select 
    b.member_id as 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 as 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.b_member_id=d.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 !='' 

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 a 
    nd sl.date_from ='' 
    and sl.date_to ='' 
ORDER BY b_member_id DESC 
私はあなたに従ってやった
+0

が、その本を投げ ' 'order clause'の 'b.member_id'列が不明です。 – satya

+0

私はasnwerをエイリアスcor b.member_idを追加し、このエイリアスを私が注文したものを使って更新しました。 – scaisEdge

+0

この '#1052 - Column 'member_id in order句はあいまいです。 – satya

関連する問題