SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to search a field with its nearest value

    Hello all
    i have a table which contains percentages of subjects.
    i.e
    Code:
     id | a | b | c | d
       1  | 15|20 |40 | 23
    Now i have another record which have subject c highest value i.e c=35
    i want to match it against the the other records which have in the range of 7 less than and 7 greater than the highest value
    (where 7<c>7 ).

    how can i make query to get the matching records.

    sample query is
    Code:
        $query="select id from percent where (subject c is in the range less than 7 and greater than 7)
    in my case if c=35 than this query should return the record with id 1 cause (7 greater than c) c=35+7=42 and in record with
    id 1 has c=40

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by semanticnotion View Post
    Hello all
    i have a table which contains percentages of subjects.
    i.e
    Code:
     id | a | b | c | d
       1  | 15|20 |40 | 23
    Wouldn't it be better to normalize that table? (id, subjectid, percentage)
    Now i have another record which have subject c highest value i.e c=35
    Another record? Where? Same table, other table, user input?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post

    Another record? Where? Same table, other table, user input?
    yes another record is of same table. why i am not normalize cause this table already contain a foreign key st_id

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What is st_id?

    How do you identify the row with the max c value to use?

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay i posted all the db design.
    i have a student table(st_id, name .....)
    and percent table (id, subject a, b, c,d, st_id)

    now first i search the percent table for specific student
    Code:
    $query="SELECT  a, b, c,d
    FROM percent
    WHERE st_id =1";
    $rs=mysql_query($query) or die(mysql_error());
    $row=mysql_fetch_assoc($rs);
    arsort($row);
    $keys = array_keys ($row);
    This will find the highest percentage and its value
    now my question is how can i find the match which is in the range 7 less and 7 greater than

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    WHERE c > 35 - 7
    AND c < 35 + 7


Tags for this Thread

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
  •