mysql - 是否有更好的方法使用 GROUP_CONCAT() 进行此特定查询?
问题描述
WordPress中的分类问题
处理 Wordpress 和 WooCommerce 分类法会遇到一些问题。至少我想要四个表给我我需要的正确数据。我对 MYSQL 的了解非常基础 - 阅读 2 天后,我不知道如何编写正确的查询!
数据库
woocommerce_attribute_taxonomies
+----------------+------------------+-----------------+
| attribute_id | attribute_name | attribute_label |
+----------------+------------------+-----------------+
| 7 | height | Height |
+----------------+------------------+-----------------+
| 2 | color | Color |
+----------------+------------------+-----------------+
term_taxonomy
+------+------------------+-----------------+
| id | taxonomy | term_id |
+------+------------------+-----------------+
| 36 | pa_color | 30 |
+------+------------------+-----------------+
| 36 | pa_height | 31 |
+------+------------------+-----------------+
terms
+---------+-------------+
| term_id | name |
+---------+-------------+
| 72 | Group 1 |
+---------+-------------+
| 73 | Group 2 |
+---------+-------------+
termmeta
+---------+-----------+-----------------+------------+
| term_id | meta_id | meta_key | meta_value |
+---------+-----------+-----------------+------------+
| 66 | 72 | _wcga_taxonomie | 7 |
+---------+-----------+-----------------+------------+
| 67 | 72 | _wcga_taxonomie | 2 |
+---------+-----------+-----------------+------------+
| 68 | 73 | _wcga_taxonomie | 2 |
+---------+-----------+-----------------+------------+
我的查询
这就是我的查询的样子..
SELECT tm.term_id
, meta_id
, name
, GROUP_CONCAT(attribute_label) AS wc_attr_label
, GROUP_CONCAT(meta_value) AS wc_attr_tax
FROM {$wpdb->prefix}termmeta AS tm
JOIN {$wpdb->prefix}terms AS t
ON t.term_id = tm.term_id
JOIN {$wpdb->prefix}woocommerce_attribute_taxonomies AS wct
ON wct.attribute_id = tm.meta_value
WHERE tm.meta_key = '_wcga_taxonomie'
GROUP
BY t.term_id
上述查询的结果
Array
(
[grouped_attributes] => Array
(
[0] => stdClass Object
(
[term_id] => 72
[meta_id] => 66
[name] => Gruppe 1
[wc_attr_label] => Height,Color
[wc_attr_tax] => 7,2
)
[1] => stdClass Object
(
[term_id] => 73
[meta_id] => 68
[name] => Gruppe 2
[wc_attr_label] => Color
[wc_attr_tax] => 2
)
)
)
我个人不喜欢使用 GROUP_CONCAT 的方式,它认为让所有值尽可能容易和干净地访问会更好。
是否可以在 [attributes] 内部的数组中设置其他对象而不进行其他查询?
我需要的?
Array
(
[grouped_attributes] => Array
(
[0] => stdClass Object
(
[term_id] => 72
[meta_id] => 66
[name] => Group 1
[attributes] => Array( Data from woocommerce_attribute_taxonomies )
)
[1] => stdClass Object
(
[term_id] => 73
[meta_id] => 68
[name] => Group 2
[attributes] => Array( Data from woocommerce_attribute_taxonomies )
)
)
)
如果任何人有更好的想法如何做到这一点,我将不胜感激!
编辑 附加信息
MySQL 版本 -> 5.6.38nmm-1log
ONLY_FULL_GROUP_BY 不知道如何检查这个,我正在阅读自动取款机!
SHOW CREATE TABLE `d02dbcff`.`a335s_termmeta`;
SELECT `meta_id`
, `term_id`
, `meta_key`
, LEFT(`meta_value`, 256)
FROM `d02dbcff`.`a335s_termmeta`
LIMIT 1000;
SHOW CREATE TABLE `d02dbcff`.`a335s_terms`;
SELECT *
FROM `d02dbcff`.`a335s_terms`
LIMIT 1000;
SHOW CREATE TABLE `d02dbcff`.`a335s_term_taxonomy`;
SELECT `term_taxonomy_id`
, `term_id`
, `taxonomy`
, LEFT(`description`,256)
, `parent`
, `count`
FROM `d02dbcff`.`a335s_term_taxonomy`
LIMIT 1000;
SHOW CREATE TABLE `d02dbcff`.`a335s_woocommerce_attribute_taxonomies`;
SELECT *
FROM `d02dbcff`.`a335s_woocommerce_attribute_taxonomies`
LIMIT 1000;
提供 MCVE
解决方案
推荐阅读
- android - 使用 Android Webview,如何将 MediaPlayer 的控制权传递给 Activity
- r - R scales::label_format 在转换为字符之前将我的数字转换为浮点数
- python - 如何将 3d 数组展平为 1d 数组
- python - 用于插值图像的快速 numpy 索引
- openstack - 如何在 Openstack 中使用分子
- codeception - 如何从命令行将环境变量传递给 Codeception YML 文件?
- python - AttributeError:模块“statsmodels”没有属性“数据集”
- python - 创建分组比例向量而不丢失行
- python - Python/Apache-Beam: How to Parse Text File To CSV?
- tensorflow - 在多个输出的情况下,Keras 的 model.predict 返回的列表中元素的顺序是什么?