MySQL:需要JOIN专业知识

时间:2012-01-06 15:14:57

标签: mysql

注意嵌套的子查询。我知道有一种更好的方法可以通过JOIN实现这一点。我只是没有专业知识来解决这个问题。

SELECT DISTINCT pm.post_id 
FROM wp_3_postmeta pm
WHERE pm.meta_key LIKE 'member_categories_%_member_categories_name'
AND pm.meta_value IN(
    SELECT tt.term_taxonomy_id
    FROM wp_3_term_taxonomy tt
    WHERE tt.parent IN(
        SELECT tt.term_taxonomy_id
        FROM wp_3_term_taxonomy tt
        WHERE tt.parent = 33)
    OR tt.parent = 33
);

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这应该是你的伎俩

SELECT DISTINCT pm.post_id 
FROM wp_3_postmeta pm
     INNER JOIN wp_3_term_taxonomy tt ON pm.meta_value = tt.term_taxonomy_id
     LEFT JOIN wp_3_term_taxonomy tt2 ON tt.parent = tt2.term_taxonomy_id
WHERE pm.meta_key LIKE 'member_categories_%_member_categories_name'
   AND (tt.parent = 33 OR tt2.parent = 33)

答案 1 :(得分:0)

我认为这应该得到你想要的东西:

SELECT DISTINCT pm.post_id
FROM wp_3_postmeta pm
JOIN wp_3_term_taxonomy tt
  ON tt.term_taxonomy_id = pm.meta_value
LEFT JOIN wp_3_term_taxonomy t2
  ON t2.term_taxonomy_id = tt.parent
WHERE pm.meta_key LIKE 'member_categories_%_member_categories_name'
  AND (t2.parent = 33 OR tt.parent = 33)