2012-05-09 56 views
0

幾個小時後,這個問題的版本一直在摔跤,而且我知道這可能是我錯過的一些小東西。在MySQL Workbench中使用會話(@variables)進行MySQL查詢,但在PHP中不能使用

我有一個基於關在這個職位的第一個答案的查詢:

How do I limit the number of rows per field value in SQL?

這不正是我希望它在MySQL工作臺做,但不會話變量永遠設置爲' 2'運行時:

mysql_query() 

in PHP。

下面是一個表, 'MYTAB' 演示該問題:

+----+--------------+---------------+ 
| id | first_column | second_column | 
+----+--------------+---------------+ 
| 1 |   1 |    1 | 
| 2 |   1 |    4 | 
| 3 |   2 |   10 | 
| 4 |   3 |    4 | 
| 5 |   1 |    4 | 
| 6 |   2 |    5 | 
| 7 |   1 |    6 | 
+----+--------------+---------------+ 

和簡化的查詢:

select 
    id, first_column, second_column, row_num 
from 
(
    select *, 
    @num := if(@first_column = first_column, 2, 1) as row_num, 
    @first_column := first_column as c 
    from mytab order by first_column,id 
) as t 
having row_num <= 1; 

從MySQL工作臺我得到這個:

+----+--------------+---------------+---------+ 
| id | first_column | second_column | row_num | 
+----+--------------+---------------+---------+ 
| 1 |   1 |    1 |  1 | 
| 3 |   2 |   10 |  1 | 
| 4 |   3 |    4 |  1 | 
+----+--------------+---------------+---------+ 

並從PHP我得到這個:

+----+--------------+---------------+---------+ 
| id | first_column | second_column | row_num | 
+----+--------------+---------------+---------+ 
| 1 |   1 |    1 |  1 | 
| 2 |   1 |    4 |  1 | 
| 3 |   2 |   10 |  1 | 
| 4 |   3 |    4 |  1 | 
| 5 |   1 |    4 |  1 | 
| 6 |   2 |    5 |  1 | 
| 7 |   1 |    6 |  1 | 
+----+--------------+---------------+---------+ 

我做錯了什麼?

非常感謝!


編輯:這裏是我的削減版的PHP代碼,因爲在現實中的問題,在一些更復雜的東西綁。

class sql_helper extends other 
{ 
    public function query_handler($sql, $error_message) 
    { 
     $this->connect(); // Not shown, but works without issue 

     $result = mysql_query($sql) or die($error_message.''.mysql_error()); 

     return $result; 
    } 

    public static function sql_result_to_assoc($sql_result) 
    { 
     $result_array = array(); 

     while($row = mysql_fetch_assoc($sql_result)) 
      $result_array[] = $row; 

     return $result_array; 
    } 

    public function sql_to_assoc($sql, $error_message) 
    { 
     $result = $this->query_handler($sql, $error_message); 

     return $this->sql_result_to_assoc($result); 
    } 
} 

$sql = " 
    select 
    id, first_column, second_column, row_num 
    from 
    (
     select *, 
     @num := if(@first_column = first_column, 2, 1) as row_num, 
     @first_column := first_column as c 
     from mytab order by first_column,id 
    ) as t 
    having row_num<=1"; 

$sql_helper = new sql_helper(); 
$result_array = $sql_helper->sql_to_assoc($sql, ''); 
+1

「我做錯了什麼?是。請發佈您的PHP代碼。 –

+0

@MarcusAdams:謝謝。這個特定的查詢實際上只需要在查詢期間存儲會話變量,所以不應該擔心瀏覽器請求。 –

回答

1

需要初始化會話變量:

SET @num:=0; 
SET @first_column:=0; 

這必須在同一個MySQL的會議和您的查詢之前完成。

0

正如Jerahian回答的那樣,他們需要參加同一會議。我已經做了一般內嵌的聲明,但你必須包裝,如果它是一個選擇和分配的「別名」,如

select 
    id, first_column, second_column, row_num 
from 
    (select *, 
      @num := if(@first_column = first_column, 2, 1) as row_num, 
      @first_column := first_column as c 
     from 
      mytab, 
      (select @first_column := 0) as Sqlvars 
     order by 
      first_column, 
      id 
    ) as t 
having 
    row_num <= 1; 

通知@first_column的非常微妙的初始化:= 0「sqlvars」的別名..然後它可以按照你的意圖使用。如果你有多個變量,只是逗號分開,就像其他列一樣......如

(select @first_column := 0, 
      @someOtherField := '2013-06-02', 
      @someString := 'testing') as Sqlvars