0

我正在创建一个链接页面,它将显示按 live_date 排序的所有链接 - 这里的问题是有两种类型的链接 - 提升和非提升 - 另一个问题是 - 列表应该按天分组,所以今天的链接将返回提升的链接然后是非提升的链接 - 然后是昨天的提升和非提升。

Fridays links

-------------
Promoted link
Promoted link
Promoted link
Normal link 
Normal link

 

Thursdays Links
--------------
Promoted link
Normal link 
Normal link 
Normal link 

等等-

我的架构:

CREATE TABLE `links` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL,
  `email` varchar(200) NOT NULL,
  `link` varchar(200) NOT NULL,
  `reciprocal_checkbox` tinyint(1) NOT NULL,
  `recip` varchar(200) NOT NULL,
  `promoted` tinyint(1) NOT NULL,
  `live` tinyint(1) NOT NULL,
  `live_time` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

我是这样想的,但我认为它不会先创建所有提升的链接,然后再创建非提升的链接(由布尔定义)

SELECT title, link, live_time FROM links WHERE promoted = 1 UNION SELECT title, link, live_time FROM links WHERE promoted = 0 GROUP BY live_date
4

2 回答 2

2

您不想分组,只想订购它们...

SELECT title, link, live_time 
FROM links 
ORDER BY live_date DESC, promoted DESC

PS - 看起来你没有live_date专栏。你想要这样的东西吗?

SELECT title, link, live_time 
FROM links 
ORDER BY DATE(live_time) DESC, promoted DESC
于 2013-05-24T10:50:46.313 回答
0

这个怎么样?

SELECT title, link, live_time FROM links
ORDER BY live_date, promoted
于 2013-05-24T10:50:46.190 回答