BD Wordpress - 表:wp_term_relationships
object_id | term_taxonomy_id| term_order 1 | 23 | 0 2 | 22 | 0 3 | 23 | 0 4 | 22 | 0 5 | 23 | 0 6 | 21 | 0
我需要计算有多少'产品'属于每个类别'并插入列'term_order'
例如:
object_id | term_taxonomy_id| term_order 1 | 23 | 3 2 | 22 | 2 3 | 23 | 3 4 | 22 | 2 5 | 23 | 3 6 | 21 | 1
我做了类似的事情,但没有更新字段' term_order':
UPDATE `letras`.`wp_term_relationships`
SET `term_order` = '2'
WHERE `wp_term_relationships`.`object_id` = '5' ;
SELECT object_id as id_objeto,
COUNT( `object_id` ) quantidade
FROM wp_term_relationships
WHERE `object_id` = `object_id`
GROUP BY `term_taxonomy_id`
答案 0 :(得分:0)
您可以使用子查询将表连接到自身:
update t
set term_order = t2.cnt
from wp_term_relationships t
join (
select term_taxonomy_id, count(*) cnt
from wp_term_relationships
group by term_taxonomy_id
) t2 on t.term_taxonomy_id = t2.term_taxonomy_id;
由于您使用的是MySQL,因此应该可以使用:
update wp_term_relationships
join (
select term_taxonomy_id, count(*) cnt
from wp_term_relationships
group by term_taxonomy_id
) t2 on wp_term_relationships.term_taxonomy_id = t2.term_taxonomy_id
set wp_term_relationships.term_order = t2.cnt;
答案 1 :(得分:0)
抱歉!
我在phpMyAdmin中执行:
update t set term_order = t2.cnt from wp_term_relationships t join ( select term_taxonomy_id, count(*) cnt from wp_term_relationships group by term_taxonomy_id ) t2 on t.term_taxonomy_id = t2.term_taxonomy_id;
错误: