Php pdo复杂查询日期

时间:2014-03-23 00:21:02

标签: php jquery mysql sql pdo

这里我有mysql数据库'track_aktivnosti'

enter image description here

现在我从jquery获得了id_akt的ajax值。现在我需要比较日期WHERE id_akt = 22,所以我需要获得最低日期并得到这样的查询:

$result = $conn->prepare("SELECT naziv FROM track_aktivnosti WHERE datum=(lowest date) AND id_akt=:id_akt");
     $result->execute(array(':id_akt' => $_POST['id_akt']));
     $result = $result->fetchAll(); 
.
.
.
after that again but with next date so like this:

$result = $conn->prepare("SELECT naziv FROM track_aktivnosti WHERE datum=(next lowest date) AND id_akt=:id_akt");
     $result->execute(array(':id_akt' => $_POST['id_akt']));
    $result = $result->fetchAll(); 

所以对我来说最大的问题是如何找到id_akt的某个值的最低日期,并从那里得到naziv,vrednost

有人有想法吗?

2 个答案:

答案 0 :(得分:1)

SELECT naziv, LOWER(datum) FROM track_aktivnosti
WHERE id_akt = id_akt

您需要查询较低的数据

答案 1 :(得分:0)

我想出了这两个解决方案。我不知道这些是不是好主意。你可以随意改变它。

第一个解决方案。

$result = $conn->prepare("SELECT datum FROM track_aktivnosti WHERE id_akt = :id_akt GROUP BY datum ORDER BY datum");

$result->execute(array(':id_akt' => $_POST['id_akt']));

$dates = $result->fetchAll();

$final_result = array();

foreach($dates as $date){

     $result = $conn->prepare("SELECT naziv, vrednost FROM track_aktivnosti WHERE id_akt = :id_akt AND datum = :datum");

     $result->execute(array(':id_akt' => $_POST['id_akt'], ':datum' => $date['datum']));

     $final_result[$date['datum']] = $result->fetchAll();
}

第二个解决方案

$result = $conn->prepare("SELECT datum FROM track_aktivnosti WHERE id_akt = :id_akt GROUP BY datum");

$result->execute(array(':id_akt' => $_POST['id_akt']));

$count = $result->rowCount();

$final_result = array();

for($i = 0; $i < $count; $i++ ){

    $limit = $i.',1';

    $result = $conn->prepare("SELECT datum FROM track_aktivnosti WHERE id_akt = :id_akt GROUP BY datum ORDER BY datum $limit");

    $result->execute(array(':id_akt' => $_POST['id_akt']));

    $result = $result->fetch(PDO::FETCH_ASSOC);

    $date = $result['datum'];

    $result = $conn->prepare("SELECT naziv, vrednost FROM track_aktivnosti WHERE id_akt = :id_akt AND datum = :datum");

    $result->execute(array(':id_akt' => $_POST['id_akt'], ':datum') => $result['datum']);

    $final_result[$date] = $result->fetchAll;

}

在两个解决方案中,结果都存储在$final_result数组中,日期为键。

我从第一个解决方案中得到的结果

array(1) { ["2013-04-01"]=> array(5) { [0]=> array(4) { ["naziv"]=> string(9) "nema nema" [0]=> string(9) "nema nema" ["vrednost"]=> string(1) "1" [1]=> string(1) "1" } [1]=> array(4) { ["naziv"]=> string(9) "nema nema" [0]=> string(9) "nema nema" ["vrednost"]=> string(1) "0" [1]=> string(1) "0" } [2]=> array(4) { ["naziv"]=> string(10) "Laza Lazic" [0]=> string(10) "Laza Lazic" ["vrednost"]=> string(1) "0" [1]=> string(1) "0" } [3]=> array(4) { ["naziv"]=> string(12) "John Deer n7" [0]=> string(12) "John Deer n7" ["vrednost"]=> string(1) "0" [1]=> string(1) "0" } [4]=> array(4) { ["naziv"]=> string(10) "Pera Peric" [0]=> string(10) "Pera Peric" ["vrednost"]=> string(1) "0" [1]=> string(1) "0" } } }