2013-05-14 78 views
0

我有一些問題,我的劇本,我無法準備查詢和腳本好好嘗試一下retrun任何錯誤:無法準備查詢並沒有錯誤輸出今天

表結構(哎呀錯誤的表,校正):

CREATE TABLE IF NOT EXISTS `razorphyn_support_users` (
`id`    BIGINT(11) UNSIGNED NOT NULL AUTO_INCREMENT, 
`name`    VARCHAR(50)    NOT NULL, 
`mail`    VARCHAR(50)    NOT NULL, 
`password`   VARCHAR(200)    NOT NULL, 
`reg_key`   VARCHAR(260)    NOT NULL, 
`tmp_password`  VARCHAR(31)    NULL, 
`ip_address`  VARCHAR(50)    NOT NULL, 
`status`   ENUM('0','1','2','3','4') NOT NULL DEFAULT '3', 
`holiday`   ENUM('0','1')    NOT NULL DEFAULT '0', 
`assigned_tickets` INT(5)  UNSIGNED NOT NULL DEFAULT 0, 
`solved_tickets` BIGINT(11) UNSIGNED NOT NULL DEFAULT 0, 
PRIMARY KEY (`id`), 
UNIQUE KEY(`mail`)) 
ENGINE=MyISAM 
DEFAULT CHARSET=utf8 
AUTO_INCREMENT=55; 

Unpreparable字符串:

$query = "SELECT `id` FROM ".$SupportUserTable." 
WHERE `status`='2' AND `holiday`='0' AND MIN(`assigned_tickets`) 
ORDER BY `solved_tickets` ASC" ; 

的完整代碼(在現實中,這只是其中的一部分,$stmt已經starterd和工作,基本上它已經連接) :

file_put_contents('ok.txt',''); 
$query = "SELECT `id` FROM ".$SupportUserTable." 
      WHERE `status`='2' AND `holiday`='0' AND MIN(`assigned_tickets`) 
      ORDER BY `solved_tickets` ASC" ; 
$prepared = $stmt->prepare($query); 
if($prepared){ 
file_put_contents('ok2.txt',''); 
if($stmt->execute()){ 
    file_put_contents('ok3.txt',''); 
    $stmt->store_result(); 
    $result = $stmt->bind_result($id); 
    file_put_contents('eafv.txt','id: '.$id); 
    if($stmt->num_rows>0){ 
     $query = "UPDATE ".$SupportTicketsTable." SET operator_id=? 
        WHERE id=? "; 
     if($prepared = $stmt->prepare($query)){ 
      if($stmt->bind_param('ii', $id,$tkid)){ 
       if($stmt->execute()){ 
        echo json_encode(array(0=>'Created')); 
       } 
       else 
        echo json_encode(array(0=>mysqli_stmt_error($stmt))); 
      } 
      else 
       echo json_encode(array(0=>mysqli_stmt_error($stmt))); 
     } 
     else 
      echo json_encode(array(0=>mysqli_stmt_error($stmt))); 
    } 
    else 
     echo json_encode(array(0=>'No Operator Available')); 
} 
else 
    echo json_encode(array(0=>mysqli_stmt_error($stmt))); 
} 
else 
    echo json_encode(array(0=>$stmt->error)); 
+0

大聲笑!!!! :D ..... – hek2mgl

+0

有人能否至少激勵減號或我必須死於無知? – Razorphyn

+0

你給這個結構的表沒有'status','holiday','assigned_tickets','resolved_tickets'字段。可能你的$ SupportUserTable有一個像「razorphyn_support_list_tickets」的值?或者你向我們展示了另一張桌子? – zkanoca

回答

2

你不能把MIN()表達式的WHERE子句中的SQL。您只能將MIN()表達式放入選擇列表,HAVING子句和ORDER BY子句中。

你想要的是:

I would like to select the operator with the minimum number of open ticket and if there are multiple operator with the same value [then] select the one with less solved tickets

這裏有一個解決方案,得到這樣的結果:

SELECT `id` FROM razorphyn_support_users 
WHERE `status` = 2 AND `holiday` = 0 
ORDER BY `assigned_tickets` ASC, `solved_tickets` ASC 
LIMIT 1 

回覆您的評論:

的限額語法,請參閱https://stackoverflow.com/a/3325580/20860或MySQL手冊中說:

For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

如果類型是字符串或枚舉,那麼使用字符串分隔符是很好的。我假定列是整數(我沒有仔細檢查上面的表定義),並刪除了字符串分隔符,因爲它們不是整數所必需的。

+0

謝謝,只是幾個問題:它是限制1還是限制0,1,並且我應該使用單引號來封裝2和0,因爲表字段是ENUM? – Razorphyn

+0

完美,非常感謝! – Razorphyn