SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help to correct syntax

    Code MySQL:
    "SELECT a.* 
    			, b.thumb_id
    			, c.amount 
    			, ( SELECT COUNT(*)
            		FROM listing_bid 
          		 	WHERE listing_id = a.id ) AS c_rows	
    		FROM listing a
    		LEFT OUTER 
    		JOIN listing_photo b
    		  ON b.listing_id = a.id
    		 AND b.key = 1
    		LEFT OUTER
    		JOIN listing_bid c
    		  ON c.listing_id = a.id ORDER BY c.id DESC LIMIT 1
    	   WHERE a.activation = 1 ";

    I want to get the highest amount so I put the ORDER BY clause in, its error.

    So I need help to put the ODER clause correctly in the query.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    write this down somewhere --

    SELECT
    FROM
    [ WHERE ]
    [ GROUP BY ]
    [ HAVING ]
    [ ORDER BY ]

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the query you have built before. I want one more thing.

    Get the highest amount (only one) in table listing_bid, the highest amount will correspond with the the highest listing_bid.id

    The possible WHERE clause is WHERE listing_bid.id = a.id

    This is what I come up with, but its not working. Please help me fix it!

    Code MySQL:
    "SELECT a.* 
    			, b.thumb_id
    			, c1.amount AS amount1
    			, ( SELECT COUNT(*)
            		FROM listing_bid
          		 	WHERE listing_id = a.id ) AS c_rows
    			, ( SELECT amount 
    			    FROM listing_bid 
    			    WHERE listing_id = a.id ORDER BY id DESC LIMIT 1  ) AS h_amount
    		FROM listing a
    		LEFT OUTER 
    		JOIN listing_photo b
    		  ON b.listing_id = a.id
    		 AND b.key = 1
    		LEFT OUTER
    		JOIN listing_bid c1
    		  ON c1.listing_id = a.id
    	   WHERE a.activation = 1"

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So weird! its working now.

    But can you tell me if I am doing it absolutely coreectly


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •