通过php在mysql中选择行作为列

时间:2012-12-03 18:39:44

标签: php mysql database

您好我试图在mysql中成功选择行作为列。

也许有人可以帮助我。

这就是我的表格

+----------+----------------------------------------------------+----------+
| brand_id | brand_name                                         | linecode | 
+----------+----------------------------------------------------+----------+
| DPQT     | 1-800 Tow Truck                                    | DER      | 
| DPQT     | 1-800 Tow Truck                                    | DCF      |
| DPQT     | 1-800 Tow Truck                                    | DCA      |
| DPQT     | 1-800 Tow Truck                                    | AAA      |
| DPQT     | 1-800 Tow Truck                                    | DDD      |
| BLGR     | 1-800-Radiator                                     | Curt     |
| BGVM     | 100+ Manufacturing/Coyote                          | ASC      |
| DPQS     | 10C Technologies Inc                               | AQW      |
| DPQS     | 10C Technologies Inc                               | ASQ      |
| FDJG     | 2 Cool AirVents                                    | CAS      |

我想让结果看起来像:

   +----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| 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   |

正如您所看到的,brand_id和brand_name是相同的,我想将行代码分组到不同的列中

最大行代码将是10.这就是我有Code1-Code10

的原因

这就是我现在正在做的事情,而且它无法正常工作。

$sql0 = 'set @num := 0';
$result = mysqli_query($dbh,"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 := @num + 1 as group_row_number 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                                    | DER   | DCF   | DCA   | AAA   | DDD   | NULL  | NULL  | NULL  | NULL  | NULL   |
| BLGR     | 1-800-Radiator                                     | NULL  | NULL  | NULL  | NULL  | NULL  | Curt  | NULL  | NULL  | NULL  | NULL   |
| BGVM     | 100+ Manufacturing/Coyote                          | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | ASC   | NULL  | NULL  | NULL   |
| DPQS     | 10C Technologies Inc                               | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | AQW   | ASQ   | NULL   |
| FDJG     | 2 Cool AirVents                                    | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | NULL  | CAS    |

我需要一种方法,这样一旦brand_id和brand_name改变,num变量就会回到0。

正如你所看到的那样,它正在做我想要它做的事情但是一旦它进入下一行,num变量就会继续上升到下一列。那么这就是添加下一行代码的地方。我假设我需要一些if语句,但我不明白该怎么做。

如果有人可以更新我的查询以便发生这种情况,我会非常感激

感谢您的时间。

3 个答案:

答案 0 :(得分:1)

你在谈论部分转置,PHP不会自动执行。但是,您可以通过一些小的修改自己完成。

在您的查询中,使用GROUP_CONCAT()

SELECT brand_id, brand_name, GROUP_CONCAT(linecode) AS codes brands GROUP BY brand_id, brand_name

在PHP中,使用explode()获取不同的代码

$codes = explode(',', $row['codes']);

注意:我对您的基础表结构做了一些假设。相应地调整查询。

答案 1 :(得分:0)

select a.name,a.value,a.code,T.code  from test1 as a INNER JOIN test1 as T on  
a.name=T.name and a.value = T.value and a.code != T.code  group by a.name;

试试这个。我假设你的品牌名称和品牌ID总是一样的。 如果它们会有所不同,他们会删除品牌名称的检查。

这只是对查询应该是什么的一个想法。你可以调整一下以获得更好的结果

答案 2 :(得分:0)

编辑:如果你必须完全在MySQL中完成,这不是很好,但是......

SELECT 
    a.brand_id, a.brand_name, b.linecode AS code1, c.linecode AS code2, d.linecode AS code3, e.linecode AS code4, f.linecode AS code4 FROM test a 
LEFT JOIN 
    test b ON b.brand_id = a.brand_id 
LEFT JOIN 
    test c ON c.brand_id = b.brand_id AND b.linecode != c.linecode
LEFT JOIN 
    test d ON d.brand_id = c.brand_id AND d.linecode != c.linecode AND d.linecode != b.linecode
LEFT JOIN 
    test e ON e.brand_id = d.brand_id AND e.linecode != d.linecode AND e.linecode != c.linecode AND e.linecode != b.linecode
LEFT JOIN 
    test f ON f.brand_id = e.brand_id AND f.linecode != e.linecode AND f.linecode != d.linecode AND f.linecode != c.linecode AND f.linecode != b.linecode
GROUP BY 
    a.brand_id
ORDER BY 
    a.brand_id

重复LEFT JOIN,直到你有10个代码列。