【问题标题】:SQL to get WordPress posts with category and post_tagSQL 获取带有类别和 post_tag 的 WordPress 帖子
【发布时间】:2020-03-27 18:00:45
【问题描述】:

我想从数据库列表中导出带有类别和标签的帖子。我试过这个Wordpress SQL: get post category and tags,但它在我的 MySQL 5.7.27 上不起作用并出现错误

Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'wpdev.c.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

我的预期结果会是一样的

+---------------+----------+----------------+
| post_id       | category | tags           |
|---------------+----------+----------------+
| 213           | news     | tag1,tag2,tag3 |
+---------------+----------+----------------+

我的声明是:

SELECT p.id, p.post_name, c.name, GROUP_CONCAT(t.name) 
FROM wp_posts p JOIN wp_term_relationships cr on (p.id=cr.object_id) 
JOIN wp_term_taxonomy ct on (ct.term_taxonomy_id=cr.term_taxonomy_id and ct.taxonomy='category') 
JOIN wp_terms c on (ct.term_id=c.term_id) 
JOIN wp_term_relationships tr on (p.id=tr.object_id) 
JOIN wp_term_taxonomy tt on (tt.term_taxonomy_id=tr.term_taxonomy_id and tt.taxonomy='post_tag') 
JOIN wp_terms t on (tt.term_id=t.term_id) 
GROUP BY p.id

提前致谢。

【问题讨论】:

  • 请在此处包含您的实际 SQL 代码以获得最佳结果。
  • ``` SELECT p.id, p.post_name, c.name, GROUP_CONCAT(t.name) FROM wp_posts p JOIN wp_term_relationships cr on (p.id=cr.@987654327 @) JOIN wp_term_taxonomy ct on (ct.term_taxonomy_id=cr.term_taxonomy_id and ct.taxonomy='category') JOIN wp_terms c on (ct.term_id=c.term_id) JOIN wp_term_relationships tr on (p.id=tr.object_id) 加入 wp_term_taxonomy tt on (tt.term_taxonomy_id=tr.term_taxonomy_id 和 tt.taxonomy='post_tag') 加入 wp_terms t on (tt.term_id =t.term_id) 按 p.id 分组 ``` @TimBiegeleisen

标签: mysql sql wordpress


【解决方案1】:

如果您将聚合函数用作GROUP_CONCAT,则必须对结果中的所有非聚合列使用GROUP BY
因此,将GROUP BY p.id 更改为GROUP BY p.id, p.post_name, c.name,错误就会消失。

要获取有关only_full_group_by 模式的更多信息,例如,您可以阅读此What are the benefits of only_full_group_by mode?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多