mysql max() query problem

Discussion in 'PHP' started by mdrobiul, Jul 7, 2009.

  1. #1
    i have the database where a field counts the visiting numbers. everytime

    I'm trying to show top visited keyword with mysql max() function.

    I can't make the query. please help me. i want to select data by checking top visited field and get it as rows.
     
    mdrobiul, Jul 7, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Does it add another row to the DB or does it update a row?

    It sounds like you need something like this.

    SELECT * FROM db ORDER BY COUNT(field) DESC LIMIT 1;

    If you are Updating a column, you would probably need something like this:

    SELECT * FROM db ORDER BY MAX(field) DESC LIMIT 1;
     
    jestep, Jul 7, 2009 IP
  3. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, second query makes sense to me. it must be the right query. actually nothing about adding another row.

    my keyword table is like this: id=1, keyword=music, visit=300; id=2, keyword=movie, visit=400

    I was trying to get these rows where order would be by max(visit) .. here movie would be top keyword...
     
    mdrobiul, Jul 8, 2009 IP
  4. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    SELECT * FROM thedatabase 
    ORDER BY MAX(thefield)
    DESC LIMIT 1;
    
    Code (sql):
    does that work?
     
    anthonywebs, Jul 8, 2009 IP