DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: Limiting number of records returned

  1. #1
    Join Date
    Jul 2001
    Posts
    10

    Question

    Hi all, I normally use ColdFusion to limit the rows returned but I need to do it with SQL only this time. I need something like this:

    SELECT id, keys
    FROM thetable
    WHERE keys LIKE '%fruit%'
    UNION
    SELECT id, keys
    FROM thetable
    WHERE keys LIKE '%apples%'

    This seems to be stacking the "apples" records first, above the "fruit" records, which is what I want (though I'd like to implicitly state that this is what I want, if anyone has any pointers).

    I also need the "fruit" part to only return 5 records, whereas the "apples" part should return everything it can get.

    Will Oracle let me do this? I can't find it in the documentation anywhere but it would be something like LIMIT for MySQL.

    Thanks in advance!

  2. #2
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    try

    SELECT id, keys
    FROM thetable
    WHERE keys LIKE '%fruit%'
    and rownum < 6
    UNION
    SELECT id, keys
    FROM thetable
    WHERE keys LIKE '%apples%'

  3. #3
    Join Date
    Jul 2001
    Posts
    10
    You're brilliant, worked like a charm -- thanks!

  4. #4
    Join Date
    Jul 2001
    Posts
    10
    This works:

    SELECT id, keys, id - 1000 "IDVal"
    FROM thetable
    WHERE keys LIKE '%fruit%'
    and rownum < 6
    UNION
    SELECT id, keys, id + 1000 "IDVal"
    FROM thetable
    WHERE keys LIKE '%apples%'
    ORDER BY "IDVal"



    [Edited by exabyte on 09-19-2001 at 12:52 PM]

Posting Permissions

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


Click Here to Expand Forum to Full Width