MySQL按联接表上的多列分组(WordPress)

MySQL按联接表上的多列分组(WordPress),mysql,wordpress,Mysql,Wordpress,我遇到了跨多个表连接并按这些表上的列分组的问题。我今天花了几个小时在工作上,但没有成功,我想我对连接或多栏分组有误解 我在这篇文章的末尾添加了一个SQL转储,这样你就可以自己查看数据库了——这可能比我的文章要解释的更好 基本上,这是一个WordPress博客,需要从数据库中取出三篇“最新文章”。复杂的是每个类别只有一篇文章,所以你不能把最近的三篇文章都放在同一个类别中 我想这应该是一个简单的“groupby”,在post id上,和category id(term_id),然后是LIMIT 3

我遇到了跨多个表连接并按这些表上的列分组的问题。我今天花了几个小时在工作上,但没有成功,我想我对连接或多栏分组有误解

我在这篇文章的末尾添加了一个SQL转储,这样你就可以自己查看数据库了——这可能比我的文章要解释的更好

基本上,这是一个WordPress博客,需要从数据库中取出三篇“最新文章”。复杂的是每个类别只有一篇文章,所以你不能把最近的三篇文章都放在同一个类别中

我想这应该是一个简单的“groupby”,在post id上,和category id(term_id),然后是LIMIT 3

在本例中,我将尝试从独特的类别中提取5篇最新文章

因此有四个表:

  • wp_posts,其中包含博客帖子。我只想回来 post\此表中的“post”类型
  • wp_术语,定义了“关键词”
  • wp_term_分类法,定义每个关键字的表格-例如。, 这是一个类别
  • wp_术语关系,将wp_术语中的“关键词”链接到帖子
您将看到下面的表格和演示数据。但很快,我的想法——显然是错误的——将是这样一个问题:

SELECT * FROM wp_posts
INNER JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.id
INNER JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_type = 'post'
GROUP BY wp_term_taxonomy.term_id wp_posts.id, 
ORDER BY post_date DESC
LIMIT 5
表中有以下数据,我希望上面的查询返回训练小狗、浴室改造、贴片玉米片和缝纫按钮。最后两个将被忽略,因为它们是重复的post ID或术语ID。但是,取而代之的是,我换了四个保险丝

查询所需数据的正确方法是什么

表wp_员额

+----+-------------------+---------------------+-----------+
| id | post_title        | post_date           | post_type |
+----+-------------------+---------------------+-----------+
|  5 | Boiling Eggs      | 2009-06-11 22:57:07 | post      |
|  6 | Boiling Eggs      | 2009-06-11 22:57:05 | revision  |
|  2 | About             | 2009-06-11 22:55:37 | page      |
|  7 | Changing Fuses    | 2009-06-11 22:57:19 | post      |
|  8 | Changing Fuses    | 2009-06-11 22:57:15 | revision  |
|  9 | Sewing Buttons    | 2009-06-11 22:57:28 | post      |
| 10 | Sewing Buttons    | 2009-06-11 22:57:25 | revision  |
| 11 | Posh Nachos       | 2009-06-11 22:57:35 | post      |
| 12 | Posh Nachos       | 2009-06-11 22:57:34 | revision  |
| 13 | Bathroom Makeover | 2009-06-11 22:57:44 | post      |
| 14 | Bathroom Makeover | 2009-06-11 22:57:41 | revision  |
| 15 | Training Puppy    | 2009-06-11 22:57:51 | post      |
| 16 | Training Puppy    | 2009-06-11 22:57:48 | revision  |
| 17 | Boiling Eggs      | 2009-06-11 22:57:07 | revision  |
| 18 | Training Puppy    | 2009-06-11 22:57:51 | revision  |
| 19 | Bathroom Makeover | 2009-06-11 22:57:44 | revision  |
| 20 | Posh Nachos       | 2009-06-11 22:57:35 | revision  |
+----+-------------------+---------------------+-----------+
表wp_术语

+---------+---------------+---------------+------------+
| term_id | name          | slug          | term_group |
+---------+---------------+---------------+------------+
|       1 | Uncategorized | uncategorized |          0 |
|       2 | Blogroll      | blogroll      |          0 |
|       3 | Food          | foot          |          0 |
|       4 | DIY           | diy           |          0 |
|       5 | Crafts        | crafts        |          0 |
|       6 | Pets          | pets          |          0 |
|       7 | puppy         | puppy         |          0 |
|       8 | dog           | dog           |          0 |
|       9 | training      | training      |          0 |
|      10 | bathroom      | bathroom      |          0 |
|      11 | rennovate     | rennovate     |          0 |
|      12 | mexican       | mexican       |          0 |
|      13 | snack         | snack         |          0 |
|      14 | fast food     | fast-food     |          0 |
+---------+---------------+---------------+------------+
表wp_术语分类法

+------------------+---------+---------------+-------------+--------+-------+
| term_taxonomy_id | term_id | taxonomy      | description | parent | count |
+------------------+---------+---------------+-------------+--------+-------+
|                1 |       1 | category      |             |      0 |     0 |
|                2 |       2 | link_category |             |      0 |     7 |
|                3 |       3 | category      |             |      0 |     2 |
|                4 |       4 | category      |             |      0 |     2 |
|                5 |       5 | category      |             |      0 |     1 |
|                6 |       6 | category      |             |      0 |     1 |
|                8 |       7 | post_tag      |             |      0 |     1 |
|                9 |       8 | post_tag      |             |      0 |     1 |
|               10 |       9 | post_tag      |             |      0 |     1 |
|               11 |      10 | post_tag      |             |      0 |     1 |
|               12 |      11 | post_tag      |             |      0 |     1 |
|               13 |      12 | post_tag      |             |      0 |     1 |
|               14 |      13 | post_tag      |             |      0 |     1 |
|               15 |      14 | post_tag      |             |      0 |     1 |
+------------------+---------+---------------+-------------+--------+-------+
表wp_术语_关系

+-----------+------------------+------------+
| object_id | term_taxonomy_id | term_order |
+-----------+------------------+------------+
|         1 |                2 |          0 |
|         2 |                2 |          0 |
|         3 |                2 |          0 |
|         4 |                2 |          0 |
|         5 |                2 |          0 |
|         6 |                2 |          0 |
|         7 |                2 |          0 |
|         6 |                1 |          0 |
|        18 |                1 |          0 |
|         8 |                1 |          0 |
|         7 |                4 |          0 |
|        10 |                1 |          0 |
|         9 |                5 |          0 |
|        12 |                1 |          0 |
|        11 |                3 |          0 |
|        14 |                1 |          0 |
|        13 |                4 |          0 |
|        16 |                1 |          0 |
|        15 |                6 |          0 |
|        17 |                1 |          0 |
|         5 |                3 |          0 |
|        15 |                8 |          0 |
|        15 |                9 |          0 |
|        15 |               10 |          0 |
|        19 |                1 |          0 |
|        13 |               11 |          0 |
|        13 |               12 |          0 |
|        20 |                1 |          0 |
|        11 |               13 |          0 |
|        11 |               14 |          0 |
|        11 |               15 |          0 |
+-----------+------------------+------------+
SQL转储

CREATE TABLE IF NOT EXISTS `wp_posts` (
  `ID` bigint(20) unsigned NOT NULL auto_increment,
  `post_author` bigint(20) unsigned NOT NULL default '0',
  `post_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_content` longtext NOT NULL,
  `post_title` text NOT NULL,
  `post_excerpt` text NOT NULL,
  `post_status` varchar(20) NOT NULL default 'publish',
  `comment_status` varchar(20) NOT NULL default 'open',
  `ping_status` varchar(20) NOT NULL default 'open',
  `post_password` varchar(20) NOT NULL default '',
  `post_name` varchar(200) NOT NULL default '',
  `to_ping` text NOT NULL,
  `pinged` text NOT NULL,
  `post_modified` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_modified_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_content_filtered` text NOT NULL,
  `post_parent` bigint(20) unsigned NOT NULL default '0',
  `guid` varchar(255) NOT NULL default '',
  `menu_order` int(11) NOT NULL default '0',
  `post_type` varchar(20) NOT NULL default 'post',
  `post_mime_type` varchar(100) NOT NULL default '',
  `comment_count` bigint(20) NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `post_name` (`post_name`),
  KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
  KEY `post_parent` (`post_parent`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;

--
-- Dumping data for table `wp_posts`
--

INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES
(5, 1, '2009-06-11 22:57:07', '2009-06-11 21:57:07', 'fadgagadf', 'Boiling Eggs', '', 'publish', 'open', 'open', '', 'boiling-eggs', '', '', '2009-06-11 22:58:03', '2009-06-11 21:58:03', '', 0, 'http://project1.searbe.co.uk/?p=5', 0, 'post', '', 0),
(6, 1, '2009-06-11 22:57:05', '2009-06-11 21:57:05', '', 'Boiling Eggs', '', 'inherit', 'open', 'open', '', '5-revision', '', '', '2009-06-11 22:57:05', '2009-06-11 21:57:05', '', 5, 'http://project1.searbe.co.uk/?p=6', 0, 'revision', '', 0),
(2, 1, '2009-06-11 22:55:37', '2009-06-11 21:55:37', 'This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.', 'About', '', 'publish', 'open', 'open', '', 'about', '', '', '2009-06-11 22:55:37', '2009-06-11 21:55:37', '', 0, 'http://project1.searbe.co.uk/?page_id=2', 0, 'page', '', 0),
(7, 1, '2009-06-11 22:57:19', '2009-06-11 21:57:19', 'fgsgfafga', 'Changing Fuses', '', 'publish', 'open', 'open', '', 'changing-fuses', '', '', '2009-06-11 22:57:19', '2009-06-11 21:57:19', '', 0, 'http://project1.searbe.co.uk/?p=7', 0, 'post', '', 0),
(8, 1, '2009-06-11 22:57:15', '2009-06-11 21:57:15', '', 'Changing Fuses', '', 'inherit', 'open', 'open', '', '7-revision', '', '', '2009-06-11 22:57:15', '2009-06-11 21:57:15', '', 7, 'http://project1.searbe.co.uk/?p=8', 0, 'revision', '', 0),
(9, 1, '2009-06-11 22:57:28', '2009-06-11 21:57:28', 'ghsghgs', 'Sewing Buttons', '', 'publish', 'open', 'open', '', 'sewing-buttons', '', '', '2009-06-11 22:57:28', '2009-06-11 21:57:28', '', 0, 'http://project1.searbe.co.uk/?p=9', 0, 'post', '', 0),
(10, 1, '2009-06-11 22:57:25', '2009-06-11 21:57:25', '', 'Sewing Buttons', '', 'inherit', 'open', 'open', '', '9-revision', '', '', '2009-06-11 22:57:25', '2009-06-11 21:57:25', '', 9, 'http://project1.searbe.co.uk/?p=10', 0, 'revision', '', 0),
(11, 1, '2009-06-11 22:57:35', '2009-06-11 21:57:35', 'hhjhdh', 'Posh Nachos', '', 'publish', 'open', 'open', '', 'posh-nachos', '', '', '2009-06-11 22:59:34', '2009-06-11 21:59:34', '', 0, 'http://project1.searbe.co.uk/?p=11', 0, 'post', '', 0),
(12, 1, '2009-06-11 22:57:34', '2009-06-11 21:57:34', '', 'Posh Nachos', '', 'inherit', 'open', 'open', '', '11-revision', '', '', '2009-06-11 22:57:34', '2009-06-11 21:57:34', '', 11, 'http://project1.searbe.co.uk/?p=12', 0, 'revision', '', 0),
(13, 1, '2009-06-11 22:57:44', '2009-06-11 21:57:44', 'hjhjdhjdjdh', 'Bathroom Makeover', '', 'publish', 'open', 'open', '', 'bathroom-makeover', '', '', '2009-06-11 22:59:19', '2009-06-11 21:59:19', '', 0, 'http://project1.searbe.co.uk/?p=13', 0, 'post', '', 0),
(14, 1, '2009-06-11 22:57:41', '2009-06-11 21:57:41', '', 'Bathroom Makeover', '', 'inherit', 'open', 'open', '', '13-revision', '', '', '2009-06-11 22:57:41', '2009-06-11 21:57:41', '', 13, 'http://project1.searbe.co.uk/?p=14', 0, 'revision', '', 0),
(15, 1, '2009-06-11 22:57:51', '2009-06-11 21:57:51', 'hjhjdjdy', 'Training Puppy', '', 'publish', 'open', 'open', '', 'training-puppy', '', '', '2009-06-11 22:59:05', '2009-06-11 21:59:05', '', 0, 'http://project1.searbe.co.uk/?p=15', 0, 'post', '', 0),
(16, 1, '2009-06-11 22:57:48', '2009-06-11 21:57:48', '', 'Training Puppy', '', 'inherit', 'open', 'open', '', '15-revision', '', '', '2009-06-11 22:57:48', '2009-06-11 21:57:48', '', 15, 'http://project1.searbe.co.uk/?p=16', 0, 'revision', '', 0),
(17, 1, '2009-06-11 22:57:07', '2009-06-11 21:57:07', 'fadgagadf', 'Boiling Eggs', '', 'inherit', 'open', 'open', '', '5-revision-2', '', '', '2009-06-11 22:57:07', '2009-06-11 21:57:07', '', 5, 'http://project1.searbe.co.uk/?p=17', 0, 'revision', '', 0),
(18, 1, '2009-06-11 22:57:51', '2009-06-11 21:57:51', 'hjhjdjdy', 'Training Puppy', '', 'inherit', 'open', 'open', '', '15-revision-2', '', '', '2009-06-11 22:57:51', '2009-06-11 21:57:51', '', 15, 'http://project1.searbe.co.uk/?p=18', 0, 'revision', '', 0),
(19, 1, '2009-06-11 22:57:44', '2009-06-11 21:57:44', 'hjhjdhjdjdh', 'Bathroom Makeover', '', 'inherit', 'open', 'open', '', '13-revision-2', '', '', '2009-06-11 22:57:44', '2009-06-11 21:57:44', '', 13, 'http://project1.searbe.co.uk/?p=19', 0, 'revision', '', 0),
(20, 1, '2009-06-11 22:57:35', '2009-06-11 21:57:35', 'hhjhdh', 'Posh Nachos', '', 'inherit', 'open', 'open', '', '11-revision-2', '', '', '2009-06-11 22:57:35', '2009-06-11 21:57:35', '', 11, 'http://project1.searbe.co.uk/?p=20', 0, 'revision', '', 0);

-- --------------------------------------------------------

--
-- Table structure for table `wp_terms`
--

CREATE TABLE IF NOT EXISTS `wp_terms` (
  `term_id` bigint(20) unsigned NOT NULL auto_increment,
  `name` varchar(200) NOT NULL default '',
  `slug` varchar(200) NOT NULL default '',
  `term_group` bigint(10) NOT NULL default '0',
  PRIMARY KEY  (`term_id`),
  UNIQUE KEY `slug` (`slug`),
  KEY `name` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;

--
-- Dumping data for table `wp_terms`
--

INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES
(1, 'Uncategorized', 'uncategorized', 0),
(2, 'Blogroll', 'blogroll', 0),
(3, 'Food', 'foot', 0),
(4, 'DIY', 'diy', 0),
(5, 'Crafts', 'crafts', 0),
(6, 'Pets', 'pets', 0),
(7, 'puppy', 'puppy', 0),
(8, 'dog', 'dog', 0),
(9, 'training', 'training', 0),
(10, 'bathroom', 'bathroom', 0),
(11, 'rennovate', 'rennovate', 0),
(12, 'mexican', 'mexican', 0),
(13, 'snack', 'snack', 0),
(14, 'fast food', 'fast-food', 0);

-- --------------------------------------------------------

--
-- Table structure for table `wp_term_relationships`
--

CREATE TABLE IF NOT EXISTS `wp_term_relationships` (
  `object_id` bigint(20) unsigned NOT NULL default '0',
  `term_taxonomy_id` bigint(20) unsigned NOT NULL default '0',
  `term_order` int(11) NOT NULL default '0',
  PRIMARY KEY  (`object_id`,`term_taxonomy_id`),
  KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `wp_term_relationships`
--

INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES
(1, 2, 0),
(2, 2, 0),
(3, 2, 0),
(4, 2, 0),
(5, 2, 0),
(6, 2, 0),
(7, 2, 0),
(6, 1, 0),
(18, 1, 0),
(8, 1, 0),
(7, 4, 0),
(10, 1, 0),
(9, 5, 0),
(12, 1, 0),
(11, 3, 0),
(14, 1, 0),
(13, 4, 0),
(16, 1, 0),
(15, 6, 0),
(17, 1, 0),
(5, 3, 0),
(15, 8, 0),
(15, 9, 0),
(15, 10, 0),
(19, 1, 0),
(13, 11, 0),
(13, 12, 0),
(20, 1, 0),
(11, 13, 0),
(11, 14, 0),
(11, 15, 0);

-- --------------------------------------------------------

--
-- Table structure for table `wp_term_taxonomy`
--

CREATE TABLE IF NOT EXISTS `wp_term_taxonomy` (
  `term_taxonomy_id` bigint(20) unsigned NOT NULL auto_increment,
  `term_id` bigint(20) unsigned NOT NULL default '0',
  `taxonomy` varchar(32) NOT NULL default '',
  `description` longtext NOT NULL,
  `parent` bigint(20) unsigned NOT NULL default '0',
  `count` bigint(20) NOT NULL default '0',
  PRIMARY KEY  (`term_taxonomy_id`),
  UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`),
  KEY `taxonomy` (`taxonomy`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;

--
-- Dumping data for table `wp_term_taxonomy`
--

INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
(1, 1, 'category', '', 0, 0),
(2, 2, 'link_category', '', 0, 7),
(3, 3, 'category', '', 0, 2),
(4, 4, 'category', '', 0, 2),
(5, 5, 'category', '', 0, 1),
(6, 6, 'category', '', 0, 1),
(8, 7, 'post_tag', '', 0, 1),
(9, 8, 'post_tag', '', 0, 1),
(10, 9, 'post_tag', '', 0, 1),
(11, 10, 'post_tag', '', 0, 1),
(12, 11, 'post_tag', '', 0, 1),
(13, 12, 'post_tag', '', 0, 1),
(14, 13, 'post_tag', '', 0, 1),
(15, 14, 'post_tag', '', 0, 1);

更新:很抱歉,我对您最初问题的理解有误。我浏览了一下,认为这是我经常看到的“每组前N名”问题的另一个例子

以下是您原始问题的解决方案,即最近的五篇文章,每个类别最多一篇文章:

SELECT t.name, p1.post_title, p1.post_date
FROM wp_term_taxonomy x
INNER JOIN wp_terms t ON (t.term_id = x.term_id)
INNER JOIN wp_term_relationships r1 ON (x.term_taxonomy_id = r1.term_taxonomy_id)
INNER JOIN wp_posts p1 ON (r1.object_id = p1.id AND p1.post_type = 'post')
LEFT OUTER JOIN (wp_term_relationships r2
    INNER JOIN wp_posts p2 ON (r2.object_id = p2.id AND p2.post_type = 'post'))
  ON (x.term_taxonomy_id = r2.term_taxonomy_id AND p1.post_date < p2.post_date)
WHERE x.taxonomy = 'category'
  AND r2.term_taxonomy_id IS NULL
GROUP BY x.term_id
ORDER BY p1.post_date DESC
LIMIT 5;
PS:非常感谢您在原始问题中发布DDL和INSERT语句!问SQL问题的人很少这样做


以下是我的第一个答案,基于我对问题的错误理解:

因此,您希望所有帖子都少于五篇具有相同类别术语和较新发布日期的帖子

您需要两次加入
posts

SELECT x.*, t.*, r1.*, p1.*
FROM wp_term_taxonomy x
 INNER JOIN wp_terms t ON (t.term_id = x.term_id)
 INNER JOIN wp_term_relationships r1 ON (r1.term_taxonomy_id = x.term_taxonomy_id)
 INNER JOIN wp_posts p1 ON (p1.id = r1.object_id)
 INNER JOIN wp_term_relationships r2 ON (r2.term_taxonomy_id = x.term_taxonomy_id)
 INNER JOIN wp_posts p2 ON (p2.id = r2.object_id AND p2.post_date > p1.post_date)
WHERE x.taxonomy = 'category'
 AND p1.post_type = 'post' AND p2.post_type = 'post'
GROUP BY x.term_id, p1.id
HAVING COUNT(*) < 5;
选择x.*、t.*、r1.*、p1*
来自wp_术语_分类法x
内部连接wp\u terms t ON(t.term\u id=x.term\u id)
内部联接wp\u term\u关系r1 ON(r1.term\u分类法\u id=x.term\u分类法\u id)
内部联接wp_将p1发布到(p1.id=r1.object_id)
内部联接wp\u term\u关系r2 ON(r2.term\u taxonomy\u id=x.term\u taxonomy\u id)
内部联接wp_在上发布p2(p2.id=r2.object_id和p2.post_date>p1.post_date)
其中x.taxonomy='category'
p1.post_type='post'和p2.post_type='post'
按x.term_id,p1.id分组
计数(*)小于5;

这是一个更简单的版本,但多亏了您的创意账单:

SELECT wt.name, wp.post_title, wp.post_date
FROM wp_term_relationships AS wtr
INNER JOIN wp_posts AS wp ON ( wtr.object_id = wp.id )
INNER JOIN wp_term_taxonomy AS wtt ON ( wtr.term_taxonomy_id = wtt.term_taxonomy_id)
INNER JOIN wp_terms AS wt ON ( wtt.term_id = wt.term_id )
WHERE wp.post_type = 'post'
and wtt.taxonomy = 'category'
group by wtt.term_id
order by wp.post_date desc;

嗨,比尔-很荣幸你能回答我的问题!我将试一试——但是从你的解释来看,我认为我对问题的描述不好(我可能应该在午夜之前问这些问题)。我想要的仅仅是最近的五篇文章,但是没有一篇文章有相同的分类术语。如果我理解正确,您的查询将返回所有“最新”的帖子,每个类别最多有五篇帖子?是的,我上面的查询就是这样做的。我不明白你原来的问题。我会尝试用一个能解决这个问题的问题来修正我的答案。准备…好的,我在上面发布了一个新的解决方案。我也留下了我的原始(错误)答案,以防将来对其他人有所帮助。:-}
SELECT wt.name, wp.post_title, wp.post_date
FROM wp_term_relationships AS wtr
INNER JOIN wp_posts AS wp ON ( wtr.object_id = wp.id )
INNER JOIN wp_term_taxonomy AS wtt ON ( wtr.term_taxonomy_id = wtt.term_taxonomy_id)
INNER JOIN wp_terms AS wt ON ( wtt.term_id = wt.term_id )
WHERE wp.post_type = 'post'
and wtt.taxonomy = 'category'
group by wtt.term_id
order by wp.post_date desc;