0

您好,我很难意识到出了什么问题..

当我在 mysql 中运行此语句时..

select brand_id,   brand_name,   
max(case when group_row_number = 1 then linecode end) Code1,   
max(case when group_row_number = 2 then linecode end) Code2,   
max(case when group_row_number = 3 then linecode end) Code3,   
max(case when group_row_number = 4 then linecode end) Code4,   
max(case when group_row_number = 5 then linecode end) Code5,   
max(case when group_row_number = 6 then linecode end) Code6,   
max(case when group_row_number = 7 then linecode end) Code7,   
max(case when group_row_number = 8 then linecode end) Code8,   
max(case when group_row_number = 9 then linecode end) Code9,   
max(case when group_row_number = 10 then linecode end) Code10 
from (   select brand_id,     brand_name,     linecode,     
@num := if(@brand_id = `brand_id` AND @brand_name = `brand_name`, @num + 1, 1) as 
group_row_number,     @brand_id := `brand_id` as dummy,   
@brand_name := `brand_name` as linedummy   from linecodes_temp ) 
src group by brand_id, brand_name order by if(linecode = '' or linecode is null,1,0), 
brand_name ASC;

我收到了我想要的结果。

  +----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| brand_id | brand_name                                         | Code1 | Code2 | Code3 | Code4 | Code5 | Code6 | Code7 | Code8 | Code9 | Code10 |
+----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| DPQT     | 1-800 Tow Truck                                    | DCF   | DCA   | AAA   | DDD   | DER   | NULL  | NULL  | NULL  | NULL  | NULL   |
| BLGR     | 1-800-Radiator                                     | Curt  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL   |
| BGVM     | 100+ Manufacturing/Coyote                          | ASC   | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL   |
| DPQS     | 10C Technologies Inc                               | ASQ   | AQW   | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL   |
| FDJG     | 2 Cool AirVents                                    | CAS   | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL   |
| DWFD     | 31 Incorporated                                    | ASD   | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL   |
| BBBB     | 3M                                                 | snoop | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL   |

太棒了。

但是当我转身并在 php.ini 中调用它时。

$result = mysql_query("select brand_id,   brand_name,   max(case when group_row_number = 1 then linecode end) Code1,   max(case when group_row_number = 2 then linecode end) Code2,   max(case when group_row_number = 3 then linecode end) Code3,   max(case when group_row_number = 4 then linecode end) Code4,   max(case when group_row_number = 5 then linecode end) Code5,   max(case when group_row_number = 6 then linecode end) Code6,   max(case when group_row_number = 7 then linecode end) Code7,   max(case when group_row_number = 8 then linecode end) Code8,   max(case when group_row_number = 9 then linecode end) Code9,   max(case when group_row_number = 10 then linecode end) Code10 from (   select brand_id,     brand_name,     linecode,      @num := if(@brand_id = `brand_id` AND @brand_name = `brand_name`, @num + 1, 1) as group_row_number,     @brand_id := `brand_id` as dummy,     @brand_name := `brand_name` as linedummy   from linecodes_temp ) src group by brand_id, brand_name order by if(linecode = '' or linecode is null,1,0), brand_name ASC",$dbh);

并做一个

while($row = mysql_fetch_assoc($result)) {

并通过桌子

我得到的结果是

 brand_id       brand_name                  Code1   Code2 Code3 Code4 Code5 to Code 10
 DPQT           1-800 Tow Truck             DER         
 BLGR           1-800-Radiator              Curt            
 BGVM           100+ Manufacturing/Coyote   ASC         
 DPQS           10C Technologies Inc        ASQ         
 FDJG           2 Cool AirVents             CAS         
 DWFD           31 Incorporated             ASD         
 BBBB           3M                          snoop           

当我执行 var_dump($row) 时,上面的 php 查询的结果会显示出来,所以我知道我在这方面没有做错任何事情

我在 mysql 和 php 中使用完全相同的查询我假设在调用我缺少的 mysql_query 时 php 中存在语法差异

如果有人可以提供帮助,我将不胜感激

感谢您的时间。

4

2 回答 2

0

在您选择的列中包含该group_row_number列并回显它们,以查看这是否真的按预期工作。

另一个区别可能在于linecodes_temp,这取决于在普通 MySQL 或 PHP 版本中是如何设置的。

我将您的子选择重写为

$sql0 = 'set @num := 0';
$result = mysql_query($sql0, $dbh);
// error handling
$sql = 'select brand_id, brand_name, @num := @num + 1 as group_row_number from linecodes_temp';
$result = mysql_query($sql, $dbh);
// error handling
while ($row = mysql_fetch_assoc($result)) {
    // process result set
}

现在 group_row_number 按预期增加。

于 2012-11-30T14:53:55.007 回答
0

您拥有的最内在的预查询

          @brand_name := `brand_name` as linedummy   
       from 
          linecodes_temp ) src 

您需要在此处添加您的 ORDER BY 子句

          @brand_name := `brand_name` as linedummy   
       from 
          linecodes_temp
       order by
          `brand_id`, `brand_name` ) src 

如果您不这样做,内部查询会以原始顺序读取数据,这可能会使它们从 ID 1、2、2、1、3、1、2、1、2 等交错排列。每次切换回来时,它都会重置您的counter 回到 1,从而覆盖您之前的 Code 1 设置。

您甚至可能需要在内部 order by 之后将计数器添加到 OUTER 结果中。

于 2012-11-30T15:03:35.043 回答