MySQL: Utilisation JOIN dans un subqery

récédent:

Les jointures peuvent être formulées dans une syntaxe explicite ([INNER | GAUCHE | OUTER | ..] ABONNEZ ... ON ...) ou de préciser les conditions dans la clause WHERE.

Comment puis-je faire référence à une table appelée dans l'externe et FROM JOIN dans la sous-requête?

Dans ce cas, je fais référence à la table avec l'alias p

SELECT 
    p.id,
    p.post_title,
    (SELECT 
            GROUP_CONCAT(DISTINCT wp_terms.name
                    SEPARATOR ',')
        FROM
            p
                JOIN
            wp_term_relationships ON (p.id = wp_term_relationships.object_id)
                LEFT JOIN
            wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
                LEFT JOIN
            wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
                AND wp_term_taxonomy.taxonomy IN ('post_tag' , 'category'))
FROM
    `post_senza_revisioni` p
WHERE
    p.post_type = 'post'
        AND p.post_status = 'publish'
        AND p.post_parent = 0
GROUP BY id , post_title
Gordon Linoff:

Je suis sûr que vous voulez qu'une corrélation:

SELECT p.id, p.post_title,
       (SELECT GROUP_CONCAT(DISTINCT wp_terms.nameSEPARATOR ',')
        FROM wp_term_relationships tr
             wp_term_taxonomy tt
             ON tr.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN
              wp_terms t
              ON t.term_id = tt.term_id AND
                 tt.taxonomy IN ('post_tag' , 'category'))
        WHERE p.id = r.object_id
       )
FROM `post_senza_revisioni` p
WHERE p.post_type = 'post' AND
      p.post_status = 'publish' AND
      p.post_parent = 0;

Je doute GROUP BYest nécessaire dans la requête externe, donc je l' ai enlevé. Si vous ne disposez en double des idvaleurs dans le ptableau, vous pouvez l' ajouter à nouveau dans - bien que cela donne à penser que idest un nom vraiment mauvais pour la colonne.

Je suppose que tu aimes

Origine http://10.200.1.11:23101/article/api/json?id=402696&siteId=1
conseillé
Classement