2017-02-03 10 views
0

私は動作するSQL文を持っていますが、Order By節を追加したい場合、クエリは機能しなくなります。SQLの順序は、どこに置くか?

以下のクエリは正常に動作します:

SELECT DISTINCT property.id 
       , property.unid 
       , property.imported 
       , property.userid 
       , CONCAT(user.firstname) as username 
       , property.url 
       , IFNULL(user.thumbpic,'temp/misc/noimage.png') as profilepic 
       , property.bedrooms 
       , property.beds 
       , type.meta_val as type 
       , property.accommodates 
       , property.price 
       , IFNULL (
         (SELECT thumbimg 
          FROM tblpropertyimages 
          WHERE pid = property.id 
          LIMIT 1 
         ) 
         , 'temp/misc/noimage.png' 
         ) image 
       , property.name as propertyname 
       , (SELECT SUM(rating) FROM tblreviews WHERE pid = property.id) as totalrating 
       , (SELECT COUNT(id) FROM tblreviews WHERE pid = property.id) as countratings 
       , location.name as cityname 
      FROM tblproperty as property 
      JOIN tbluser as user 
      ON property.userid = user.id 
      JOIN tblcommon as type 
      ON property.type = type.id 
      LEFT 
      JOIN tblpropertyamenities as p_amenities 
      ON property.id = p_amenities.pid 
      JOIN tbllocation as location 
      ON location.id = property.city 
      WHERE property.status = 'Active' 
      AND user.status = 'Active' 
      AND property.price >= 0 
      AND property.price <= 10000 
      LIMIT 9 
     OFFSET 0 

をしかし、私は文の最後に次の行を追加する場合:property.priceのASC BY

ORDER

次にクエリこのORDER BY句がエラーの原因となっている理由は何ですか?

+8

のランダムシーケンスまたは順序を取得するためにバインドされているようまた、order byなしLIMITは意味がありません。 – jarlh

+0

ちなみに、tblpropertyamenitiesの列が選択されていないように見えますが、なぜそれが含まれているのかわかりません。 – Strawberry

答えて

1

あなたはLIMITORDER BYを配置する必要があります:

MYSQL - ORDER BY & LIMIT

SELECT DISTINCT 
property.id,property.unid,property.imported,property.userid, 
CONCAT(user.firstname) as username,property.url, 
IFNULL(user.thumbpic,'temp/misc/noimage.png') as profilepic, 
property.bedrooms,property.beds,type.meta_val as 
type,property.accommodates,property.price, 
IFNULL((select thumbimg from tblpropertyimages where pid=property.id 
limit 1),'temp/misc/noimage.png') as image, 
property.name as propertyname,(select sum(rating) from tblreviews where   
pid=property.id) as totalrating, 
(select count(id) from tblreviews where pid=property.id) as countratings, 
location.name as cityname from tblproperty as property join tbluser as 
user on property.userid=user.id 
join tblcommon as type on property.type=type.id 
left join tblpropertyamenities as p_amenities on 
property.id=p_amenities.pid 
join tbllocation as location on location.id=property.city 
WHERE property.status='Active' and user.status='Active' 
and property.price >= 0 and property.price <= 10000 
ORDER BY property.price ASC limit 9 offset 0 

あなた一次のでクエリは、そのようなものである必要があり、その後、あなたが限度でフィルタリングします。

私はそれが助けてくれることを願っています!

+0

ありがとうございます、それは動作します – user3186034

0

LIMITの前に次のように記述します。あなたが制限句の前にデータ

 AND property.price <= 10000 
     ORDER BY ... //Here 
     LIMIT 9