2016-10-05 5 views
0

mysqlの結果を取得したいが、私が新しいコンピュータプログラミングまたはコンピュータデータベースであるためにクエリが機能しない。mysqlに参加または連結するときに構文エラーが発生する

SELECT guid, post_name 
FROM `wp_posts` 
WHERE `post_parent` = 0 and `post_type` = 'post' and post_title != 'Auto Draft'  

戻り値:

guid     post_name 
http://localhost/?p=21 post-2-title 
http://localhost/?p=12 post-4-title 

そして

SELECT guid as children_url, post_parent as post_parent 
FROM `wp_posts` 
WHERE `post_parent` != 0 and `post_type` = 'attachment' and post_title != 'Auto Draft' 

戻り値:私はいくつかのクエリを試みたが成功しなかった

children_url        post_parent 
http://localhost/attachment-post-1   12 
http://localhost/attachment-post-2   12 
http://localhost/attachment-post-3   12 
http://localhost/attachment-post-4   12 
http://localhost/attachment-post-5   12 
http://localhost/attachment-post-6   12 
http://localhost/attachment-post-7   12 
http://localhost/attachment-post-8   12 
http://localhost/attachment-post-9   21 
http://localhost/attachment-post-10   21 
http://localhost/attachment-post-11   21 
http://localhost/attachment-post-12   21 
http://localhost/attachment-post-13   21 
http://localhost/attachment-post-14   21 
http://localhost/attachment-post-15   21 
http://localhost/attachment-post-16   21 

SELECT guid as children_url, post_parent as post_parent 
FROM `wp_posts` 
WHERE `post_parent` != 0 and `post_type` = 'attachment' and post_title != 'Auto Draft' 
    and SELECT guid, post_name FROM `wp_posts` 
     WHERE `post_parent` = 0 and `post_type` = 'post' and post_title != 'Auto Draft' 

はエラーを発生させます:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT guid, post_name FROM wp_posts WHERE post_parent = 0 and post_type =' at line 1

私のような結果欲しい:

children_url        post_parent    post_paraent_guid  post_parent_title 
http://localhost/attachment-post-1   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-2   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-3   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-4   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-5   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-6   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-7   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-8   12     http://localhost/?p=12 post-2-title 
http://localhost/attachment-post-9   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-10   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-11   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-12   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-13   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-14   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-15   21     http://localhost/?p=21 post-4-title 
http://localhost/attachment-post-16   21     http://localhost/?p=21 post-4-title 

すべてのヘルプはいただければ幸いです。

+0

を試してみて、そして2つの列のみを求めいただきありがとうございます。それとも私はあなたの質問を間違って読みましたか? –

+0

あなたは正しいです、私はデータベースクエリについての私の限られた知識のために申し訳ありません – Rhezashan

答えて

1

あなたは4つの列をしたい、この

SELECT 
    pc.guid as children_url, 
    pc.post_parent as post_parent, 
    pp.guid as post_paraent_guid, 
    pp.post_title as post_parent_title 
FROM 
    wp_posts pc 
    left join wp_posts pp on pp.ID=pc.post_parent 
WHERE 
    pc.post_type = 'attachment' 
    and pc.post_title != 'Auto Draft' 
ORDER BY 
    pc.guid 
+0

とにかくcategoryId 2からの検索を制限する方法もありますか? – Rhezashan

+0

where where条件 'WHERE pc.post_type =' attachment ' とpc.post_title!='自動ドラフト 'とcategoryId = 2'を追加します。 –

+0

申し訳ありませんが、' where句 'のUnknown columns' categoryId ' – Rhezashan

関連する問題