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的方式,它认为使所有值都尽可能容易且整洁地访问会更好。
是否可以设置包裹在[属性]内的数组中的其他对象而无需执行其他查询?
我需要什么?
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不知道如何检查,我在看atm!
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