简体   繁体   English

从表中获取,切换行和列,并按另一个列值进行过滤

[英]Fetch from table, switch rows and columns and filter by another column value

Presumably this question has been answered elsewhere but i've not managed to accomplish what im trying to do so far (reeeaal beginner over here). 大概这个问题已经在其他地方回答了,但是到目前为止我还没有完成我想做的事情(这里是初学者)。

What im trying to do is retrieve data from a table called "wp_iphorm_form_entry_data" which looks something like this: 我试图做的是从名为“ wp_iphorm_form_entry_data”的表中检索数据,该表看起来像这样:

%/ element_id / value      /%
-----------------------------
%/ 4          / first name /%
%/ 5          / last name  /%
%/ 42         / e-mail     /%

Then switch the rows and columns and rename them, something like this: 然后切换行和列并重命名它们,如下所示:

%/ 4 (f-name)  / 5 (l-name) / 42 (e-mail) /%
--------------------------------------------
  first name  / last name  / e-mail      /%

And finally filter the entries by another "element_id". 最后用另一个“ element_id”过滤条目。 Thank you in advance, any takers? 预先感谢您,任何参加者?

This is the way it was done before pivot function existed 这是在枢轴功能存在之前完成的方法

SELECT userGroupingID
       MAX(case when element_Id=4 then value else null end) as `First Name`,
       MAX(case when element_Id=5 then value else null end) as `Last Name`,       
       MAX(case when element_Id=42 then value else null end) as `e-mail`
FROM  wp_iphorm_form_entry_data
GROUP BY userGroupingID 

UserGroupingID is some value which denotes grouped elements like a userID or a groupID for the set of elements. UserGroupingID是一些值,它表示分组的元素,例如用户ID或元素集的groupID。 Without this, I can't see how you could accomplish what you're requesting for more than 1 row of data. 没有这个,我看不到如何完成多于1行数据的请求。 of course with mysql's extension to the group by statement... if you don't care which values are returned for each element ID, you don't need userGroupingID in teh select, or a group by at all.. 当然还有mysql对group by语句的扩展...如果您不在乎为每个元素ID返回哪个值,则在select中根本不需要userGroupingID,也根本不需要group by。

Pivot could do this simpler: example MySQL pivot table Pivot可以使此操作更简单:示例MySQL数据透视表

But you will have the same problem with it. 但是您将遇到同样的问题。 What denotes how the elements are grouped? 什么表示元素如何分组?

What you have to do there is, just run fetch all your results you need and initialize them to array, then print them the way you want 您所要做的就是,只需获取所需的所有结果并将其初始化为数组,然后以所需的方式打印它们即可

$el_id=[]; $val=[];   //create an array to store your results from db
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error".mysqli_error($link)); //conection
$sql="select element_id,value  from wp_iphorm_form_entry_data";
$result = $link->query($sql); //execute the query.

while($row = mysqli_fetch_array($result)) {  //store your results into arrays 
$el_id = $row["element_id"]; 
 $val  = $row["value"];
} 

print "<table><tr><th>$el_id[0] (f-name)</th><th>$el_id[1] (l-name)</th><th>$el_id[2] (e-mail)</th></tr>";
//foreach($el_id as $index=>$elementId){ // i comment this since your example is not clear to me
 echo '<tr><td>$val[0]</td><td>$val[1]</td><td>$val[]</td>';
//}

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

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