2013-10-16 29 views
2

BD WordPress的 - 表:wp_term_relationshipsMYSQL更新選擇 - 升級列

 
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

您可以加入該表本身使用子查詢:

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; 
+0

錯誤: 1064 - SQL中有錯誤語法;檢查與您的MySQL服務器版本相對應的手冊,以便在第3行的wp_term_relationships t join(select term_taxonomy_id,count(*)cn)附近使用正確的語法。 – user2718396

+0

@ user2718396 - 查看更新。 DBMS。 – sgeddes

0

對不起!

我EXCUTE在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; 

錯誤:

1064 - 你在你的SQL語法錯誤;請檢查與您的MySQL服務器版本對應的手冊,以找到正確的語法,以便在第3行使用「from wp_term_relationships t join(select term_taxonomy_id,count(*)cn'