-1

I have a database with Name (aseguradora), Price (Precact) and Date (Ultdato). $matriu1 is a variable (table from datatable). Id is a variable (date selection from user)

I want to select the max and min price and date from both to show it, but the date not show the correct date. I think that I need to create a subquery, but I'm not sure. Any help? THANKS!

$sqle= "SELECT MIN(Precact), Ultdato 
FROM $matriu1[3] 
WHERE Aseguradora = '$aseguradora' AND Id <= '$fecha'"; // Seleccionem el valor mínim

$sqlf= "SELECT * MAX(Precact), Ultdato 
FROM $matriu1[3] 
WHERE Aseguradora = '$aseguradora' AND Id <= '$fecha'"; // Seleccionem el valor màxim
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Dabi
  • 3
  • 3
  • this code is **vulnerable** to **sql injection**, so please use only **prepared statemenst with parameters** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Jan 23 '21 at 20:23

1 Answers1

0

You can use next approach:

SELECT Precact, Ultdato 
FROM $matriu1[3]
JOIN (
    MIN(Precact) minPrecact FROM $matriu1[3]
    WHERE
        Aseguradora = '$aseguradora' AND
        Id <= '$fecha'
) minPrecact ON minPrecact = Precact
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39