2

BD ワードプレス - テーブル: 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` 
4

2 に答える 2

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;
于 2013-10-16T20:41:58.427 に答える
0

ごめん!

私はphpMyAdminで実行します:

アップデート
term_order = t2.cnt を設定します。
wp_term_relationships から
  加入 (
    term_taxonomy_id を選択し、count(*) cnt
    wp_term_relationships から
    term_taxonomy_id でグループ化
) t.term_taxonomy_id = t2.term_taxonomy_id の t2;

エラー:

1064 - SQL 構文にエラーがあります。MySQL サーバーのバージョンに対応するマニュアルで、3 行目の「from wp_term_relationships t join ( select term_taxonomy_id, count(*) cn」の近くで使用する正しい構文を確認してください

于 2013-10-17T12:37:21.673 に答える