私は3つのテーブル:tco_articles
,tco_module_eostext
およびtco_articles_modules
を持っています。私のtco_articles
には一意のid
キーがあります。各記事ごとに1つ。私のtco_module_eostext
には、各記事に属する一意のinstance_id
があります。 私のtco_articles_modules
にはすべてarticle_id
が含まれていますが、他の表では9倍の数値が使用されています。MySQLでクエリを実行するときにテーブルを接続する
だから私はあなたがtco_module_eostext
に照会すると、空返すことinstance_id
とarticle_id
を持つことができます。
正しい記事の正しい本文を返すクエリを作成したいと思います。
は、これまでのところ私が持っている:あなたはarticle_id
は同じではなくinstance_id
あることがわかります
Array
(
[0] => Array
(
[instance_id] => 928615
[article_id] => 129396
)
[1] => Array
(
[instance_id] => 928616
[article_id] => 129396
)
[2] => Array
(
[instance_id] => 928617
[article_id] => 129396
)
[3] => Array
(
[instance_id] => 928618
[article_id] => 129396
)
:
はglobal $wpdb;
$posts = array();
$ids = $wpdb->get_results('SELECT DISTINCT instance_id, article_id FROM tco_articles_modules', ARRAY_A);
これは、のようなすべてのインスタンスとIDの配列を返します。あなたは
$wpdb->get_results('SELECT body FROM tco_module_eostext WHERE instance_id=928617 ', ARRAY_A);
を置くときあなたは空のかも知れませんが、
$wpdb->get_results('SELECT body FROM tco_module_eostext WHERE instance_id=928618 ', ARRAY_A);
のためにあなたは、いくつかの本文テキストを持つことができます。
これは私の問題です。私はそれらのすべてを通過し、空でないものを除き、それらに正しい記事を割り当てる必要があります。私はtco_module_eostext
テーブルからbody
テキストを追加したいのですが今
Array
(
[129396] => Array
(
[id] => 129396
[headline] => Bla bla bla title
[intro] => This is cool article intro
[needs_review] => 0
[published] => 2014-12-16 09:17:00
[unpublished] =>
[hidden] => 0
[no_single_page] => 0
[permalink] => link-perma-link-here
[internal_slug] =>
[type_id] => 3
[thread_id] => 0
[news_id] => 0
[header_game_id] => 0
[puff_hh_id] => 0
[puff_title] =>
[hdrcol_id] => 900
[review_queued] =>
[lock_timeout] => 0
[created] => 2014-12-16 09:17:00
[updated] => 2015-01-16 13:51:30
[created_by] => 84142
[updated_by] => 84142
)
...
:私は記事
のようなものを返すforeach ($ids as $key => $value) {
$instance_ID = $value['instance_id'];
$article_ID = $value['article_id'];
$article_out = $wpdb->get_results('SELECT * FROM tco_articles WHERE id='.$article_ID.' ', ARRAY_A);
$posts[$article_ID] = $article_out[0];
}
出力に管理されます。
私はこれを自動的に行うために、またはこの時点でこれを実行してから$posts
アレイに追加するために使用できるクエリはありますか?
180000+個の投稿がある場合、foreachのクエリ方法はちょっと遅いです。
何か助けていただければ幸いです。
だから、' tco_module_eostext'は、複数の 'body'テキストを持つことができます。複数ある場合、どちらを取得したいのですか?最新のもの(instance_id DESCによる)? –
'tco_module_eostext'テーブルの各' instance_id'には 'body'テキストが添付されています(各idに一意です)。しかし、1つの 'article_id'に対して9つの' instance_id'を保持する 'tco_articles_modules'を通らない限り、このテキストがどの記事に属しているかを知る方法はありません。この9つのうちの1つだけが正しいものであり、残りは空です。 'article_id'は' tco_articles'テーブルの 'id'と同じです。 –
したがって、 'instance_ids'の9つのうちの1つのために常に' tco_module_eostext'に1行しか存在しないことは確かです。 –