need help in this query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Muddasir
    New Member
    • Jun 2007
    • 49

    need help in this query

    Hello everyone.
    guyz can you please help me in this query

    id | name | lastname | status | hrs

    1 qwe sss 1 5
    2 rtyy zax 1 5
    3 bvgf hyv 1 5
    4 bvgd vbcf 1 10
    5 cvxx hopy 1 10
    6 bcxd loiu 1 10


    now what i want to achieve is that......
    first four records when hrs>1 or hrs<5 and last four records when hrs>5 and hrs< 10
    and how can i manage same scenario when there will be lots of record

    looking forward for your response

    Regards
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    Originally posted by Muddasir
    Hello everyone.
    guyz can you please help me in this query

    id | name | lastname | status | hrs

    1 qwe sss 1 5
    2 rtyy zax 1 5
    3 bvgf hyv 1 5
    4 bvgd vbcf 1 10
    5 cvxx hopy 1 10
    6 bcxd loiu 1 10


    now what i want to achieve is that......
    first four records when hrs>1 or hrs<5 and last four records when hrs>5 and hrs< 10
    and how can i manage same scenario when there will be lots of record

    looking forward for your response

    Regards
    Unless you are not explaining what you wish to do well enough, I do not understand what you really want.

    Consider your first query: you want the first 4 records when hrs>1 or hrs<5. In your above table, each and every row matches this criterion. So there is no basis for extracting only the first 4 rows using only the problem statement that you show here.

    Same goes for your second query.

    You need to state more precisely what you want, and how it would be extrapolated to when there are more rows in the table (do you still want the first 4 and the last 4 respectively?). And then it can be decided if your problem statement will allow for a unique solution.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      1.) use "select" to retrieve the wanted records
      2.) use "limit" to retrieve only the needed number.

      If you are not sure how to do that, read in the SQL manual about these words (or google for them, or read an online SQL course for beginner etc.)

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Try to use UNION clause here:

        [code=mysql]

        SELECT * from table1 WHERE hrs > 1 AND hrs < 5
        UNION
        SELECT * FROM table1 WHERE hrs > 5 AND hrs < 10

        [/code]

        Comment

        • Muddasir
          New Member
          • Jun 2007
          • 49

          #5
          thanks a lot ppl

          okay I will try to put my question more clearly.

          Normally if we want to fetch any record against any specific value we use e.g. "WHERE hr=2" and in the result se we will get a row of record having hr=2, and no result when colum hr doesnot have value= 2 right !.

          I am asking is it possible that we can get results even when colum hr doesn't have value=2. can we get the results against colum (hr) value = 5..... means if not exact value is entered can we get result near to that value....say if 2 is entered we can get all result where hr=5 if 7 is entered we can get all results where hr=10. and so on..

          hope this time i am clear in my question

          sorry for my bad english

          regards

          Comment

          • coolsti
            Contributor
            • Mar 2008
            • 310

            #6
            In order for you (or for us to help you) to make a query to do what you wish to do, you must first be able to describe precisely, either in words or mathematically in the form of an algorithm, what it is you wish to do.

            For example, if you wish to select only the records where hrs is equal to the value 3, you of course have the where clause "where hrs = 3".

            If you wish to select only the records where hrs is "within 2 of the value 3", this translates mathematically to a value of hrs that lie between and including 3-2 and 3+2. And this then gives you the where clause "where hrs >= (3 - 2) and hrs <= (3 + 2). Here I write the where clause with 3-2 instead of 1 and 3+2 instead of 5, because what you might wish to do is to have the value central value of 3 be a variable which is changed from query to query.

            What I am saying is, try to state as mathematically as possible the "general rule" that you want to use to select your rows, and then it is easier for us to help.

            Comment

            • amitpatel66
              Recognized Expert Top Contributor
              • Mar 2007
              • 2358

              #7
              Are you looking at something like:

              get the records if hrs is > 2 and <= 5? - this will give you all the revords with hr = 3 or 4 or 5

              hrs > 5 and hrs <= 10 -- this will give you all the records with hr = 6 or 7 or 8 or 9 or 10

              So what are you exactly looking at?..I am sorry but still I am not able to understand what exactly you are looking at.

              Comment

              Working...