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

Php 无法从MySQL查询中获得正确的输出,php,mysql,sql,Php,Mysql,Sql,我有一个数据库,其中包括以下表格: 使用者 类别 文章 在我们的网站内,我们有一个名为“编辑精选”的栏目,其中有5篇由编辑在该领域选出的最佳文章 编辑器必须设置is_Recommendated=yes,还必须设置Recommendated_位置,可以是1、2、3、4或5;因此,他们将被放置在网站上的1-5个位置之一 文章也有一个开始日期,这意味着作者可以写一篇文章,将其指定为“是”和“推荐位置”3,然后将其设置为明天晚上9点。因此,这篇文章只会出现在明天,当它出现时,它应该放在编辑选择的3个框中

我有一个数据库,其中包括以下表格:

使用者 类别 文章 在我们的网站内,我们有一个名为“编辑精选”的栏目,其中有5篇由编辑在该领域选出的最佳文章

编辑器必须设置is_Recommendated=yes,还必须设置Recommendated_位置,可以是1、2、3、4或5;因此,他们将被放置在网站上的1-5个位置之一

文章也有一个开始日期,这意味着作者可以写一篇文章,将其指定为“是”和“推荐位置”3,然后将其设置为明天晚上9点。因此,这篇文章只会出现在明天,当它出现时,它应该放在编辑选择的3个框中

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

身份证号码:123 建议:是 推荐位置=3 开始日期=06-05-2016 09:00:00假设这是昨天 目前占据第三的位置

我还有一篇文章:

身份证号码:456 建议:是 推荐位置=3 开始日期=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
试试这个:

在查询中按降序使用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

我希望您能找到解决方案。

您没有聚合功能,所以如果您需要的话,最终不需要使用distinct进行分组

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
如果您只想为每个推荐的_位置提供一篇文章,您应该使用

(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)  

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

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 ...

但是,如果两篇或更多文章具有相同的开始日期,您将以这种方式获得所有文章…

您还应该进行结束日期比较,即使这意味着手动为开始日期添加1天。这样,您就可以在开始日期和结束日期之间使用类似日期的内容……这样就不会混淆应该返回哪些行。@Turo有您想要的解决方案。这是你试图解决的一个不平凡的问题。阅读关于试图解决这类问题的文章谢谢@AgRizzo这正是问题所在。现在把2+2放在一起。@TinTran但这对我们的系统没有意义,结束日期不清楚,实际上不存在。一旦一篇文章是活的,它就需要保持活的。谢谢你,不幸的是,它没有给我我想要的结果。谢谢。不幸的是,这并没有产生正确的结果。我需要按照推荐的位置获取最新的项目;然而,这个查询获取它们并随机放置它们;我现在在1-5中有两个项目被设置为推荐位置=3我还没有检查这是否有效,但这不是服务器上的一个巨大负载吗?我们的网站上有超过10万的用户,我正在努力寻找优化的方法。我知道我们可以缓存,但一般来说,这么长的查询很可怕,至少可以这么说。它正在工作。。。我在考虑这样做是否正确。好吧,如果这个答案有效的话。。那么我认为这至少是有用的。。我认为在这种情况下,每个类别有一个元素。。带限制的select union是简单或唯一的解决方案。我感谢您在这方面的帮助。在我们找到长期答案的同时,它为我们节省了短期时间。