SQL子查询(从三个表中选择数据)

时间:2016-12-13 15:57:55

标签: mysql sql

我正在使用此查询从mysql获取数据。可以改进吗?每个查询大约需要0.2秒......

我发现了一些其他方式,例如左连接但我无法通过一个查询获取所有数据。

或者我是否需要使用两个查询?

  1. 选择p.id,p.post_date,p.post_content,p.post_title,p.post_name,p.guid

  2. for loop - > likes_count,dislikes_count,post_views_count,rate where post_id = p.id?

  3. SELECT p.id, p.post_date, p.post_content, p.post_title, p.post_name, p.guid, 
    (SELECT guid FROM wp_posts WHERE id = (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key =  "_thumbnail_id")) AS thumbnail_url, 
    (SELECT GROUP_CONCAT(DISTINCT term_taxonomy_id) FROM wp_term_relationships WHERE object_id = p.id) AS term_taxonomy_id, 
    (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key =  "likes_count" LIMIT 1) AS likes_count, 
    (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key =  "dislikes_count" LIMIT 1) AS dislikes_count, 
    (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key =  "post_views_count" LIMIT 1) AS post_views_count, 
    (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key =  "rate" LIMIT 1) AS rate 
    FROM wp_posts AS p INNER JOIN wp_term_relationships AS t ON p.id = t.object_id  
    WHERE p.post_status="publish" 
    AND p.ping_status="open" 
    AND t.term_taxonomy_id = ? GROUP BY p.id LIMIT ?,?
    

    [EDIT-1] [表格结构] 1

    [EDIT-2] 输出

    {"id":11111,"post_date":"2016-01-02T05:46:58.000Z","post_content":"contenttttttt","post_title":"titleeeee","post_name":"postname","guid":"1255322","thumbnail_url":"thumb1957.jpg","term_taxonomy_id":"7","likes_count":"1","dislikes_count":"1","post_views_count":"538","rate":"50"}
    

0 个答案:

没有答案