簡體   English   中英

MySQL - 子查詢排序 MySQL

[英]MySQL - subquery sorting MySQL

SELECT 
    post_type, post_status, 
    COUNT(post_id) AS posts, 
    SUM(post_word_count) AS word_count, 
    SUM(post_tag_p_count) AS text_p_count, 
    SUM(post_text_p_length) AS text_p_length, 
    MAX(post_text_p_length) AS max_text_p_length, 
    (
        SELECT MIN(post_text_p_length) 
        FROM $table_name_posts 
        WHERE post_text_p_length !=''
    ) AS min_text_p_length, 
    SUM(post_text_p_length_characters_without_spaces) AS text_p_length_characters_without_spaces, 
    MAX(post_text_p_length_characters_without_spaces) AS max_text_p_length_characters_without_spaces, 
    (
        SELECT MIN(post_text_p_length_characters_without_spaces) 
        FROM $table_name_posts 
        WHERE post_text_p_length_characters_without_spaces !=''
    ) AS min_text_p_length_characters_without_spaces, 
    SUM(post_text_p_length_characters) AS text_p_length_characters, 
    MAX(post_text_p_length_characters) AS max_text_p_length_characters, 
    (
        SELECT MIN(post_text_p_length_characters) 
        FROM $table_name_posts 
        WHERE post_text_p_length_characters !=''
    ) AS min_text_p_length_characters, 
    SUM(post_tag_h1_count) AS text_h1_count, 
    SUM(post_text_h1_length) AS text_h1_length, 
    MAX(post_text_h1_length) AS max_text_h1_length, 
    (
        SELECT MIN(post_text_h1_length) 
        FROM $table_name_posts 
        WHERE post_text_h1_length !=''
    ) AS min_text_h1_length, 
    SUM(post_text_h1_length_characters_without_spaces) AS text_h1_length_characters_without_spaces, 
    MAX(post_text_h1_length_characters_without_spaces) AS max_text_h1_length_characters_without_spaces, 
    (
        SELECT MIN(post_text_h1_length_characters_without_spaces) 
        FROM $table_name_posts 
        WHERE post_text_h1_length_characters_without_spaces !=''
    ) AS min_text_h1_length_characters_without_spaces, 
    SUM(post_text_h1_length_characters) AS text_h1_length_characters, 
    MAX(post_text_h1_length_characters) AS max_text_h1_length_characters, 
    (
        SELECT MIN(post_text_h1_length_characters) 
        FROM $table_name_posts 
        WHERE post_text_h1_length_characters !=''
    ) AS min_text_h1_length_characters, 
    SUM(post_tag_h2_count) AS text_h2_count, 
    SUM(post_text_h2_length) AS text_h2_length, 
    MAX(post_text_h2_length) AS max_text_h2_length, 
    (
        SELECT MIN(post_text_h2_length) 
        FROM $table_name_posts 
        WHERE post_text_h2_length !=''
    ) AS min_text_h2_length, 
    SUM(post_text_h2_length_characters_without_spaces) AS text_h2_length_characters_without_spaces, 
    MAX(post_text_h2_length_characters_without_spaces) AS max_text_h2_length_characters_without_spaces, 
    (
        SELECT MIN(post_text_h2_length_characters_without_spaces) 
        FROM $table_name_posts 
        WHERE post_text_h2_length_characters_without_spaces !=''
    ) AS min_text_h2_length_characters_without_spaces, 
    SUM(post_text_h2_length_characters) AS text_h2_length_characters, 
    MAX(post_text_h2_length_characters) AS max_text_h2_length_characters, 
    (
        SELECT MIN(post_text_h2_length_characters) 
        FROM $table_name_posts 
        WHERE post_text_h2_length_characters !=''
    ) AS min_text_h2_length_characters, 
    SUM(post_tag_h3_count) AS text_h3_count, 
    SUM(post_text_h3_length) AS text_h3_length, 
    MAX(post_text_h3_length) AS max_text_h3_length, 
    (
        SELECT MIN(post_text_h3_length) 
        FROM $table_name_posts 
        WHERE post_text_h3_length !=''
    ) AS min_text_h3_length, 
    SUM(post_text_h3_length_characters_without_spaces) AS text_h3_length_characters_without_spaces, 
    MAX(post_text_h3_length_characters_without_spaces) AS max_text_h3_length_characters_without_spaces, 
    (
        SELECT MIN(post_text_h3_length_characters_without_spaces) 
        FROM $table_name_posts 
        WHERE post_text_h3_length_characters_without_spaces !=''
    ) AS min_text_h3_length_characters_without_spaces, 
    SUM(post_text_h3_length_characters) AS text_h3_length_characters, 
    MAX(post_text_h3_length_characters) AS max_text_h3_length_characters, 
    (
        SELECT MIN(post_text_h3_length_characters) 
        FROM $table_name_posts 
        WHERE post_text_h3_length_characters !=''
    ) AS min_text_h3_length_characters
FROM $table_name_posts
WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'future')
GROUP BY post_type, post_status
ORDER BY word_count DESC;

分組子查詢的問題,選擇MIN的時候,完全選擇最小值,不解決GROUP BY post_type, post_status

你應該使用條件聚合來過濾你需要的行並避免子查詢,而不是混合聚合列和子查詢
通過這種方式,您可以使用 group by 而不會與與子查詢相關的差異聚合級別發生沖突

SELECT post_type
    , post_status
    , COUNT(post_id) AS posts
    , SUM(post_word_count) AS word_count
    , SUM(post_tag_p_count) AS text_p_count
    , SUM(post_text_p_length) AS text_p_length
    , MAX(post_text_p_length) AS max_text_p_length


    , MIN (CASE WHEN post_text_p_length !='' THEN post_text_p_length ELSE 100000 END) AS min_text_p_length

    , SUM(post_text_p_length_characters_without_spaces) AS text_p_length_characters_without_spaces
    , MAX(post_text_p_length_characters_without_spaces) AS max_text_p_length_characters_without_spaces

    , MIN( CASE WHEN post_text_p_length_characters_without_spaces !='' THEN post_text_p_length_characters_without_spaces ELSE 100000 END)AS min_text_p_length_characters_without_spaces

    , SUM(post_text_p_length_characters) AS text_p_length_characters
    , MAX(post_text_p_length_characters) AS max_text_p_length_characters


    , MIN( CASE WHEN post_text_p_length_characters !='' THEN post_text_p_length_characters ELSE 100000 END ) AS min_text_p_length_characters
    .....
    ....
    , MIN ( CASE WHEN  post_text_h3_length_characters !='' THEN post_text_h3_length_characters ELSE 100000 END)
FROM $table_name_posts
WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'future')
GROUP BY post_type, post_status
ORDER BY word_count DESC";

其中 100000 只是一個虛擬值,用於在 case 匹配中避免 null 值的最小值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM