繁体   English   中英

使用两个表组合 SQL 查询

[英]Combine SQL Query using two tables

是否可以将查询合并为一次调用? UPDATE wp_term_taxonomy必须在第一个 UPDATE wp_posts 之后运行。

这是查询,我刚刚开始进行查询:

UPDATE  `wp_posts` p 
    LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
    LEFT OUTER JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id
    LEFT OUTER JOIN wp_terms t ON t.term_id = x.term_id
    SET  p.post_type =  'recipe'
    WHERE p.post_type = 'post'
    AND t.slug = 'recipes' 
    OR t.slug = 'cocktails' 
    OR t.slug = 'breakfast' 
    OR t.slug = 'cookies-recipes' 
    OR t.slug = 'desserts'
    OR t.slug = 'main-course'
    OR t.slug = 'sides'
    OR t.slug = 'soups'
    OR t.slug = 'starters'

UPDATE wp_term_taxonomy SET taxonomy = 'recipe-categories' WHERE term_taxonomy_id IN (53,57,72,75,125,138,177);

考虑使用CASEIF表达式进行SET赋值。 下面还根据您似乎打算做的事情调整WHERE逻辑:

UPDATE `wp_posts` p 
LEFT OUTER JOIN wp_term_relationships r 
   ON r.object_id = p.ID 
LEFT OUTER JOIN wp_term_taxonomy x 
   ON x.term_taxonomy_id = r.term_taxonomy_id 
LEFT OUTER JOIN wp_terms t 
   ON t.term_id = x.term_id 
SET p.post_type = 'recipe',
    x.taxonomy = CASE 
                      WHEN x.term_taxonomy_id IN (53,57,72,75,125,138,177)
                      THEN 'recipe-categories'
                      ELSE x.taxonomy
                 END
WHERE p.post_type = 'post' 
  AND (   t.slug = 'recipes' 
       OR t.slug = 'cocktails' 
       OR t.slug = 'breakfast' 
       OR t.slug = 'cookies-recipes' 
       OR t.slug = 'desserts' 
       OR t.slug = 'main-course' 
       OR t.slug = 'sides' 
       OR t.slug = 'soups'
       OR t.slug = 'starters')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM