Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. select before and after question

select before and after question

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eli Nurman
    wrote on last edited by
    #1

    Hi, I Wonder if it possible to retrieve from an SQL server a recored plus its 5 recored before anf after. for example: When I search a product with serial-number "100" i would like to retrieve 11 recorods from serial-number "095" until "105", of course after a "ORDER BY" statement of the serial-number field

    L C 2 Replies Last reply
    0
    • E Eli Nurman

      Hi, I Wonder if it possible to retrieve from an SQL server a recored plus its 5 recored before anf after. for example: When I search a product with serial-number "100" i would like to retrieve 11 recorods from serial-number "095" until "105", of course after a "ORDER BY" statement of the serial-number field

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      ???

      WHERE ID BETWEEN 95 AND 105

      depending on the database the limit values will or will not be included (then change to 94 AND 106) :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


      1 Reply Last reply
      0
      • E Eli Nurman

        Hi, I Wonder if it possible to retrieve from an SQL server a recored plus its 5 recored before anf after. for example: When I search a product with serial-number "100" i would like to retrieve 11 recorods from serial-number "095" until "105", of course after a "ORDER BY" statement of the serial-number field

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        where serial-number between (100-5) and (100+5)
        order by seriual-number

        Does that do the trick? :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        E 1 Reply Last reply
        0
        • C Chris Meech

          where serial-number between (100-5) and (100+5)
          order by seriual-number

          Does that do the trick? :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

          E Offline
          E Offline
          Eli Nurman
          wrote on last edited by
          #4

          Thanks. But this could be a good solution when querying a numeric field , my main problem is when querying a varchar field like product descriptions

          J 1 Reply Last reply
          0
          • E Eli Nurman

            Thanks. But this could be a good solution when querying a numeric field , my main problem is when querying a varchar field like product descriptions

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #5

            Assuming that your description field is unique:

            SELECT table.*
            FROM table,(
            SELECT id
            FROM table
            WHERE description = 'whatever'
            ) temp
            WHERE table.id BETWEEN (temp.id - 5) AND (temp.id + 5)

            "When did ignorance become a point of view" - Dilbert

            E 1 Reply Last reply
            0
            • J Jorgen Andersson

              Assuming that your description field is unique:

              SELECT table.*
              FROM table,(
              SELECT id
              FROM table
              WHERE description = 'whatever'
              ) temp
              WHERE table.id BETWEEN (temp.id - 5) AND (temp.id + 5)

              "When did ignorance become a point of view" - Dilbert

              E Offline
              E Offline
              Eli Nurman
              wrote on last edited by
              #6

              thanks. this is an excelent solutions but when a few descriptions are 'whatever' is will appear a few times....

              N 1 Reply Last reply
              0
              • E Eli Nurman

                thanks. this is an excelent solutions but when a few descriptions are 'whatever' is will appear a few times....

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                Oh come on! Do you want someone to right it for you? You have more than enough to figure out the rest on your own.


                I know the language. I've read a book. - _Madmatt

                E M 2 Replies Last reply
                0
                • N Not Active

                  Oh come on! Do you want someone to right it for you? You have more than enough to figure out the rest on your own.


                  I know the language. I've read a book. - _Madmatt

                  E Offline
                  E Offline
                  Eli Nurman
                  wrote on last edited by
                  #8

                  ok, i am just climbing down the tree...

                  1 Reply Last reply
                  0
                  • N Not Active

                    Oh come on! Do you want someone to right it for you? You have more than enough to figure out the rest on your own.


                    I know the language. I've read a book. - _Madmatt

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    Wheres that spoon icon?

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups