[英]Data filter from database with SQL
I want to filter a specific set of data from DB with SQL according to its release date, yeah I am developing a movie site. 我想根据发布日期从SQL过滤一个特定的数据集,是的,我正在开发一个电影网站。 I want to get those data which gonna come in future at the top with ASC order then the past data, I mean released data with DESC order and I got that with the help of StackOverflow community. 我希望得到那些将来会以ASC顺序排在最前面的数据然后是过去的数据,我的意思是用DESC命令发布数据,我在StackOverflow社区的帮助下得到了这些数据。 But It feels like I am having another problem that, I want that, the data which passed its released date, its gonna stay top for first 15 days then follow my rule which I said at the first. 但感觉我有另一个问题,我想要的,通过其发布日期的数据,它将在前15天保持最高,然后遵循我在第一次说的规则。
My table structure: 我的表结构:
CREATE TABLE `movies` (
`id` int(255) NOT NULL,
`title` varchar(255) NOT NULL,
`imdbID` varchar(255) NOT NULL,
`poster` varchar(255) NOT NULL,
`year` varchar(255) NOT NULL,
`rel_date` date NOT NULL,
`main_menu_id` int(255) NOT NULL
)
--
ALTER TABLE `movies`
ADD PRIMARY KEY (`id`);
ALTER TABLE `movies`
MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=131;
COMMIT;
I have been using this to keep upcoming data at the top with ASC order then released data with DESC order by rel_date 我一直在用这个来保存ASC订单的顶部数据,然后通过rel_date以DESC订单发布数据
SELECT t.* FROM movies t
where main_menu_id=".$menu_id."
ORDER BY
DATE(t.rel_date) >= DATE(NOW()) DESC ,
IF(DATE(t.rel_date)<DATE(NOW()),t.rel_date,DATE(NULL)) DESC ,
t.rel_date ASC
Now I expect the data which released stays at the top for the first 15 days then follow my recent code. 现在我希望发布的数据在前15天保持在最顶层,然后按照我最近的代码。 I know It was a horrible description coz it's my first post. 我知道这是一个可怕的描述因为这是我的第一篇文章。 Sorry about that. 对于那个很抱歉。
I created this code in Postgresql, but you need converte function now() to mysql. 我在Postgresql中创建了这个代码,但你需要将函数now()转换为mysql。
create table test ( id serial, release date );
insert into test (release) values ( to_date('01/04/2019', 'dd-mm-yyyy') );
insert into test (release) values ( to_date('02/04/2019', 'dd-mm-yyyy') );
insert into test (release) values ( to_date('03/04/2019', 'dd-mm-yyyy') );
insert into test (release) values ( to_date('15/04/2019', 'dd-mm-yyyy') );
insert into test (release) values ( to_date('16/04/2019', 'dd-mm-yyyy') );
insert into test (release) values ( to_date('17/04/2019', 'dd-mm-yyyy') );
select
release
from (
(
select
0 priority,
release
from test
where
release >= (now() - INTERVAL '15 DAY')::date
)
union
(
select
1 priority,
release
from test
where
release < (now() - INTERVAL '15 DAY')::date
)) foo
order by
priority;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.