我有这张桌子
id bigint(20) unsigned Incrément automatique
form_id mediumint(8) unsigned [0]
entry_id bigint(20) unsigned
meta_key varchar(255) NULL
meta_value longtext NULL
它显示如下数据:
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-031e">id</th>
<th class="tg-031e">form_id</th>
<th class="tg-031e">entry_id</th>
<th class="tg-yw4l">meta_key</th>
<th class="tg-yw4l">meta_value</th>
</tr>
<tr>
<td class="tg-031e">3</td>
<td class="tg-031e">1</td>
<td class="tg-031e">1</td>
<td class="tg-yw4l">14</td>
<td class="tg-yw4l">Address 1</td>
</tr>
<tr>
<td class="tg-031e">6</td>
<td class="tg-031e">1</td>
<td class="tg-031e">1</td>
<td class="tg-yw4l">20</td>
<td class="tg-yw4l">1</td>
</tr>
<tr>
<td class="tg-yw4l">7</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">21</td>
<td class="tg-yw4l">2018-16-05</td>
</tr>
<tr>
<td class="tg-yw4l">8</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">23</td>
<td class="tg-yw4l">Product 1</td>
</tr>
<tr>
<td class="tg-yw4l">11</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">2</td>
<td class="tg-yw4l">14</td>
<td class="tg-yw4l">Address 2</td>
</tr>
<tr>
<td class="tg-yw4l">14</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">2</td>
<td class="tg-yw4l">20</td>
<td class="tg-yw4l">1</td>
</tr>
<tr>
<td class="tg-yw4l">15</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">2</td>
<td class="tg-yw4l">21</td>
<td class="tg-yw4l">2018-16-05</td>
</tr>
<tr>
<td class="tg-yw4l">16</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">2</td>
<td class="tg-yw4l">23</td>
<td class="tg-yw4l">Product2</td>
</tr>
<tr>
<td class="tg-yw4l">20</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">3</td>
<td class="tg-yw4l">14</td>
<td class="tg-yw4l">Address3</td>
</tr>
<tr>
<td class="tg-yw4l">21</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">3</td>
<td class="tg-yw4l">20</td>
<td class="tg-yw4l">1</td>
</tr>
<tr>
<td class="tg-yw4l">22</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">3</td>
<td class="tg-yw4l">21</td>
<td class="tg-yw4l">2018-16-05</td>
</tr>
<tr>
<td class="tg-yw4l">23</td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">3</td>
<td class="tg-yw4l">23</td>
<td class="tg-yw4l">Prodcut3</td>
</tr>
</table>
我需要的是使用选择请求:
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
</style>
<table class="tg">
<tr>
<th class="tg-031e">meta_value1</th>
<th class="tg-031e">meta_value2</th>
<th class="tg-031e">meta_value3</th>
<th class="tg-031e">meta_value4</th>
<th class="tg-031e"></th>
</tr>
<tr>
<td class="tg-031e">Address 1</td>
<td class="tg-031e">1</td>
<td class="tg-031e">2018-16-05</td>
<td class="tg-031e">Product1</td>
<td class="tg-031e"></td>
</tr>
<tr>
<td class="tg-031e">Address 2</td>
<td class="tg-031e">1</td>
<td class="tg-031e">2018-16-05</td>
<td class="tg-031e">Product2</td>
<td class="tg-031e"></td>
</tr>
<tr>
<td class="tg-031e">Address 3</td>
<td class="tg-031e">1</td>
<td class="tg-031e">2018-16-05</td>
<td class="tg-031e">Prodcut3</td>
<td class="tg-031e"></td>
</tr>
<tr>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
</tr>
</table>
提前谢谢
答案 0 :(得分:0)
一种方法是按条目对meta_value进行分组,并将meta_value连接成一个字符串:
SELECT entry_id, GROUP_CONCAT(meta_value) AS meta_string
FROM your_table
GROUP BY entry_id
这将返回:
| 1 |地址1,1,2018-16-05,产品1 |
| 2 |地址2,1,2018-16-05,产品2 | 等
然后,您可以展开meta_string并构建表格,或者您可以将html作为分隔符放在查询中,如:
GROUP_CONCAT(meta_value ASC SEPARATOR '</td></tr><td>')
这将返回一个形式为:
的元数据Address 1</td><td>1</td><td>2018-16-05</td><td>Product 1
事实上,您需要在查询中添加tr和第一个/最后一个td / td的包装(参见下一个)或在您的代码中添加。
CONCAT('<tr><td>, GROUP_CONCAT(meta_value ASC SEPARATOR '</td></tr><td>', </td></tr>)) AS full_html_row
如果您需要meta_key,请执行相同的操作,但是这样:
SELECT entry_id, GROUP_CONCAT(meta_key) AS keys_string
FROM your_table
WHERE entry_id = 1
GROUP BY entry_id
UNION
SELECT entry_id, GROUP_CONCAT(meta_value) AS meta_string
FROM your_table
GROUP BY entry_id
另一种方法是,使用CASE来获得分隔列中的结果。但是,这不如在PHP或其他内容中旋转原始数据那样有效。
SELECT entry_id,
MAX(CASE WHEN meta_key = 14 THEN meta_value ELSE NULL END) AS meta_value1,
MAX(CASE WHEN meta_key = 20 THEN meta_value ELSE NULL END) AS meta_value2,
MAX(CASE WHEN meta_key = 21 THEN meta_value ELSE NULL END) AS meta_value3,
MAX(CASE WHEN meta_key = 23 THEN meta_value ELSE NULL END) AS meta_value4
FROM your_table
GROUP BY entry_id