简体   繁体   English

通过PHP在MySQL中选择行作为列

[英]Selecting rows as columns in mysql through php

Hello i am trying to successfully select rows as columns in mysql. 您好,我正在尝试成功选择行作为mysql中的列。

Maybe someone can help me. 也许有人可以帮助我。

This is what my table looks like 这是我的桌子的样子

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

and i am trying to get the results to look like : 我正在尝试使结果看起来像:

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

As you can see when the brand_id and the brand_name are the same i want to group the line codes into separate columns 如您所见,当brand_id和brand_name相同时,我想将行代码分组到单独的列中

The max line codes will ever have is 10. That is why i have Code1-Code10 最大行代码将永远是10。这就是为什么我有Code1-Code10

This is what i am doing right now, and it is not working. 这是我现在正在做的,并且没有用。

$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");

and the results i am getting back are 我得到的结果是

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

I need a way so that once the brand_id and brand_name change the num variable goes back to 0. 我需要一种方法,使brand_id和brand_name更改后,num变量将恢复为0。

As you can see it is doing what i want it to do but once it goes to the next row the num variable continues going up to the next column. 如您所见,它正在执行我想要的操作,但是一旦转到下一行,num变量将继续向上移动到下一列。 So then that is where the next line code gets added. 这样便可以添加下一行代码。 I am assuming i need a if statement in there some where but i don't understand how to do it. 我假设我在某些地方需要if语句,但是我不知道该怎么做。

If anyone could update my query so that this happens i would be very thankful 如果有人可以更新我的查询,以便这种情况发生,我将非常感激

Thank you for your time. 感谢您的时间。

You're talking about partial transposing , which PHP does not do automagically. 您正在谈论的是部分转置 ,PHP不会自动完成。 However, you could do this yourself with some small modifications. 但是,您可以自己进行一些小的修改。

In your query, use GROUP_CONCAT() : 在查询中,使用GROUP_CONCAT()

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

In PHP, use explode() to get your different codes : 在PHP中,使用explode()获得不同的代码

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

Note: I've made some assumptions about your underlying table structure. 注意:我对您的基础表结构做了一些假设。 Adjust the query accordingly. 相应地调整查询。

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;

Try this. 尝试这个。 I have assumed that your brand name and brand id are always going to be the same. 我假设您的品牌名称和品牌ID总是一样。 If they are going to be different , they remove the check for brand names. 如果它们会有所不同,则会删除对品牌名称的检查。

This one is just an idea of what the query should be . 这只是查询的概念。 U can tweak this to get better results 你可以调整以获得更好的结果

EDIT: if you MUST do it entirely in MySQL, this isn't pretty, but... 编辑:如果您必须完全在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

Repeat the LEFT JOINs until you have 10 code columns. 重复LEFT JOIN,直到您有10个代码列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM