简体   繁体   中英

MySQL and asp select highest row in area

I have an MySQL table with 3 fields per entry. x and y are primary keys and one value. I want to select the highest row in a given area.

As an example: The table looks like this:

I want to make a request to get the row with the highest entry with x >=2 and x <4 and y >=1 and y <= 3. In this example this would be the row with x = 2, y = 3 and value = 0,6.

Currently, the code in my .asp file looks like this:

if reqType="getDBMaxValue" then
  resp="[ "
  delim=""
  'sSQL="SELECT MAX(value) AS value FROM "&tableName&" WHERE x >= "&xStart&" AND x <= "&xEnd&" AND y >= "&yStart&" AND y <= "&yEnd
  sSQL="SELECT MAX(value) AS value FROM "&tableName&" WHERE x >= "&xStart&" AND x <= "&xEnd&" AND y >= "&yStart&" AND y <= "&yEnd
  set rs=Conn.Execute(sSQL)
  do until rs.EOF
    obj="{ " & chr(34) & "value" & chr(34) & ": " & chr(34) & rs.Fields("value") & chr(34)
    obj=obj & "} "
    resp=resp & delim & obj
    delim=" , "
  rs.MoveNext
  loop
  resp=resp & " ]"

  rs.Close
  response.ContentType="application/json"
  Response.Write(resp)
end if

This returns the correct value, but I don't get the x and y.

With this code:

if reqType="getDBvalues" then
  resp="[ "
  delim=""
  sSQL="SELECT * FROM "&tableName&" WHERE x >= "&xStart&" AND x <= "&xEnd&" AND y >= "&yStart&" AND y <= "&yEnd
  set rs=Conn.Execute(sSQL)
  do until rs.EOF
    obj="{ " & chr(34) & "x" & chr(34) & ": " & chr(34) & rs.Fields("x") & chr(34)
    obj=obj & ", " & chr(34) & "y" & chr(34) & ": " & chr(34) & rs.Fields("y") & chr(34)
    obj=obj & ", " & chr(34) & "value" & chr(34) & ": " & chr(34) & rs.Fields("value") & chr(34)
    obj=obj & "} "
    resp=resp & delim & obj
    delim=" , "
  rs.MoveNext
  loop
  resp=resp & " ]"

  rs.Close
  response.ContentType="application/json"
  Response.Write(resp)
end if

I receive the whole row (or rows).

Does someone know how I can write down the MySQL statement to get the row with the max value?

SELECT * FROM my_table WHERE x BETWEEN... AND y BETWEEN... ORDER BY value DESC LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM