您好,我很难意识到出了什么问题..
当我在 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 中存在语法差异
如果有人可以提供帮助,我将不胜感激
感谢您的时间。