Results 1 to 3 of 3
  1. #1
    Join Date
    May 2007
    Posts
    139

    Unanswered: what must be done to final result appear in the order of second statement in each que

    Queries of the kind of

    SELECT * FROM Products WHERE Product_ID IN (SELECT Product_ID FROM SidesFeaturedList ORDER BY AA)
    SELECT * FROM Products WHERE Product_ID IN (SELECT Product_ID FROM ThreeFeaturedList)

    what must be done to final result appear in the order of second statement in each query?
    or this I say is the default?

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    Hi,

    Firstly the order by clause in a subquery is irrelevant and should be removed. What you need to do is to have the ORDER BY statement on the primary query. Using the IN is filtering your rows but you actually need to join the two tables so that you can use the column you want to order by:

    Code:
    SELECT p.* FROM Products p, SlidesFeaturedList s WHERE p.Product_ID = s.Product_ID
    ORDER BY s.AA
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    two minor but important improvements:
    Code:
    SELECT s.AA
         , p.* 
      FROM Products AS p
    INNER
      JOIN SlidesFeaturedList AS s 
        ON s.Product_ID = p.Product_ID
    ORDER 
        BY s.AA
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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