Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何返回所选行的最大值?_Mysql_Select_Row - Fatal编程技术网

Mysql 如何返回所选行的最大值?

Mysql 如何返回所选行的最大值?,mysql,select,row,Mysql,Select,Row,我正在尝试选择行(而不是列)中的最新日期 在这样的表中,它必须是“添加的文章”或“上次修改的文章” Id | ... | ... | articles_date_added | articles_last_modified | ... 我真正的疑问是: select a.articles_id, a.authors_id, a.articles_date_added, a.articles_last_modified, IF(a.articles_last_modified &g

我正在尝试选择行(而不是列)中的最新日期

在这样的表中,它必须是“添加的文章”或“上次修改的文章”

Id | ... | ... | articles_date_added | articles_last_modified | ...
我真正的疑问是:

select 
  a.articles_id, a.authors_id, a.articles_date_added,
  a.articles_last_modified,
  IF(a.articles_last_modified >= a.articles_date_added,
     a.articles_last_modified,
     a.articles_date_added) as latestdate,
  ad.articles_viewed,
  ad.articles_name, ad.articles_head_desc_tag,
  COUNT(vh.articles_id) as total_votes,
  SUM(v.vote_value)/COUNT(v.vote_value) AS vote_avg,
  au.authors_name, td.topics_name, a2t.topics_id 
from
  " . TABLE_ARTICLES . " a
  left join " . TABLE_AUTHORS . " au using(authors_id)
  left join VOTE_HISTORY vh using (articles_id)
  left join VOTE v using (vote_id),
  " . TABLE_ARTICLES_DESCRIPTION . " ad,
  " . TABLE_ARTICLES_TO_TOPICS . " a2t
  left join " . TABLE_TOPICS_DESCRIPTION . " td using(topics_id)
where 
  (a.articles_date_available IS NULL or
  to_days(a.articles_date_available) <= to_days(now())) and
  a.articles_status = '1' and a.articles_id = a2t.articles_id and
  ad.articles_id = a2t.articles_id and
  ad.language_id = '" . (int)$languages_id . "'
  and td.language_id = '" . (int)$languages_id . "'
  and a2t.topics_id = '" . (int)$current_topic_id . "' and
  au.authors_id = '" . (int)$_GET['filter_id'] . "' 
GROUP BY a.articles_id 
ORDER BY latestdate desc
但它重新计算了1054—order子句中的未知列“latestdate”

为什么?


我使用的是MySql 5.0。

因为添加是一种修改,如果您上次修改的a.articles与插入行时添加的a.articles包含相同的日期,这将是一个好主意

UPDATE `articles`
SET `articles_last_modified` = `articles_date_added`
WHERE `articles_last_modified` = '0000-00-00 00:00:00'

ALTER TABLE `articles`
CHANGE COLUMN `articles_last_modified` `articles_last_modified` 
TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

这样,您就不需要在clausule

乐意报告问题的情况下进行排序。(花了几个小时)技巧的语法是按“latestdate”描述的顺序。仅使用“”
UPDATE `articles`
SET `articles_last_modified` = `articles_date_added`
WHERE `articles_last_modified` = '0000-00-00 00:00:00'

ALTER TABLE `articles`
CHANGE COLUMN `articles_last_modified` `articles_last_modified` 
TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;