无法从MySQL查询中获得正确的输出

时间:2016-06-08 19:05:15

标签: php mysql sql

我有一个包含以下表格的数据库:

  • 用户
  • 分类
  • 文章

在我们的网站内,我们有一个名为"编辑选择"这个领域的编辑选择了5篇最好的文章。

编辑必须设置" is_recommended = yes"以及" recommended_location"可以是1,2,3,4或5;因此,他们将被放置在网站上的1-5个展示位置之一。

文章还有一个" start_date"意思是作者可以写一篇文章,将其指定为is_recommended = yes和recommended_location = 3,然后将其设置为明天晚上9点。因此,这篇文章只会在明天出现,当它出现时,它应该位于编辑选择的3个方框中。

有时我们可能会有如下文章:

  • ID:123
  • is_recommended:是
  • recommended_location = 3
  • start_date = 06-05-2016 09:00:00(让我们说这是昨天)

目前正处于第3位。

我还有另一篇文章:

  • ID:456
  • is_recommended:是
  • recommended_location = 3
  • start_date = 07-05-2016 09:00:00(这是今天,今天已经是上午11点)

但是我的查询仍然显示ID:123;虽然我希望它显示插槽#3中最新的(意思是456)

有人可以在下面的查询中告诉我我做错了什么,我怎样才能确保每个插槽都选择了最新的项目?

这是查询:

select * 
from (
    select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
    from article 
    left join user on article.created_by = user.id 
    left join category on category.id = article.category_id 
    where article.status='active' 
    AND is_recommended='yes' 
    AND article.start_date<='".date('Y-m-d H:i:s')."' 
    AND recommended_location in (1,2,3,4,5) 
    order by start_date desc
 ) as x 
 group by recommended_location 
 limit 5

3 个答案:

答案 0 :(得分:2)

你没有聚合功能所以你不需要分组(如果这是你需要的话,最终会使用不同的)

select * 
from (    select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
    from article 
    left join user on article.created_by = user.id 
    left join category on category.id = article.category_id 
    where article.status='active' 
    AND is_recommended='yes' 
    AND article.start_date<='".date('Y-m-d H:i:s')."' 
    AND recommended_location in (1,2,3,4,5) 
    order by start_date desc
 ) as x 
 limit 5

如果您只想为每个recommended_location添加一篇文章,则应使用

(select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
from article 
left join user on article.created_by = user.id 
left join category on category.id = article.category_id 
where article.status='active' 
AND is_recommended='yes' 
AND article.start_date<='".date('Y-m-d H:i:s')."' 
AND recommended_location = '1' 
order by start_date desc limit 1)
union
(select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
from article 
left join user on article.created_by = user.id 
left join category on category.id = article.category_id 
where article.status='active' 
AND is_recommended='yes' 
AND article.start_date<='".date('Y-m-d H:i:s')."' 
AND recommended_location = '2' 
order by start_date desc limit 1)
union 
(select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
from article 
left join user on article.created_by = user.id 
left join category on category.id = article.category_id 
where article.status='active' 
AND is_recommended='yes' 
AND article.start_date<='".date('Y-m-d H:i:s')."' 
AND recommended_location = '3' 
order by start_date desc limit 1)   
union 
(select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
from article 
left join user on article.created_by = user.id 
left join category on category.id = article.category_id 
where article.status='active' 
AND is_recommended='yes' 
AND article.start_date<='".date('Y-m-d H:i:s')."' 
AND recommended_location = '4' 
order by start_date desc limit 1)  
union 
(select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
from article 
left join user on article.created_by = user.id 
left join category on category.id = article.category_id 
where article.status='active' 
AND is_recommended='yes' 
AND article.start_date<='".date('Y-m-d H:i:s')."' 
AND recommended_location = '5' 
order by start_date desc limit 1)  

答案 1 :(得分:1)

首先进行聚合,然后加入所需的数据

select x.recommended_location, x.start_date, ... 
from
 ( select article.recommended_location, max(article.start_date) as  start_date 
    from article 
     where article.status='active' 
     AND is_recommended='yes' 
     AND article.start_date<='".date('Y-m-d H:i:s')."' 
    AND recommended_location in (1,2,3,4,5) 
    group by article.recommended_location
 ) as x 
inner join article on x.recommended_location = artice.recommended_location    
and x.start_date = article.start_date
inner join ...

但是,如果2篇或更多文章具有相同的start_date,您将以这种方式获得所有这些...

答案 2 :(得分:0)

试试这个:

在查询中使用降序的article.id

所以你的查询是这样的:

select * 
from (
    select article.*, user.username, category.title as ctitle, user.firstname, user.lastname, category.slug as cslug, category.category_id as pid 
    from article 
    left join user on article.created_by = user.id 
    left join category on category.id = article.category_id 
    where article.status='active' 
    AND is_recommended='yes' 
    AND article.start_date<='".date('Y-m-d H:i:s')."' 
    AND recommended_location in (1,2,3,4,5) 
    order by article.ID desc, recommended_location desc, start_date desc
 ) as x 
 group by recommended_location 
 limit 5

我希望你能得到解决方案。