Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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_Drupal - Fatal编程技术网

Mysql 为什么不通过小组合作来订购?

Mysql 为什么不通过小组合作来订购?,mysql,drupal,Mysql,Drupal,我有新闻表,有4个类别的新闻,我想得到每个类别的最新新闻(按日期排序)。我使用了以下查询: SELECT node.nid AS nid, term_data.tid AS term_data_tid, node.type AS node_type, node.vid AS node_vid, node.title AS node_title, node_data_field_blog_overview.field_blog_overview_value AS no

我有新闻表,有4个类别的新闻,我想得到每个类别的最新新闻(按日期排序)。我使用了以下查询:

SELECT node.nid AS nid,
   term_data.tid AS term_data_tid,
   node.type AS node_type,
   node.vid AS node_vid,
   node.title AS node_title,
   node_data_field_blog_overview.field_blog_overview_value AS node_data_field_blog_overview_field_blog_overview_value,
   node_data_field_blog_overview.field_blog_overview_format AS node_data_field_blog_overview_field_blog_overview_format,
   term_data.name AS term_data_name,
   term_data.vid AS term_data_vid,
   users.name AS users_name,
   users.uid AS users_uid,
   node.created AS node_created,
   node_data_field_blog_overview.field_feed_author_name_value AS node_data_field_blog_overview_field_feed_author_name_value,
   node.sticky AS node_sticky
 FROM node node 
 LEFT JOIN term_node term_node ON node.vid = term_node.vid
 LEFT JOIN term_data term_data ON term_node.tid = term_data.tid
 LEFT JOIN content_field_image node_data_field_image ON node.vid = node_data_field_image.vid
 LEFT JOIN content_type_blog node_data_field_blog_overview ON node.vid = node_data_field_blog_overview.vid
 INNER JOIN users users ON node.uid = users.uid
 WHERE (node.type in ('blog')) AND (node.status <> 0)
 GROUP BY term_data_name order by node_created
  LIMIT 5
选择node.nid作为nid,
term_data.tid作为term_data_tid,
node.type作为节点类型,
node.vid作为node_vid,
node.title作为node_title,
节点\数据\字段\博客\概览。字段\博客\概览\值作为节点\数据\字段\博客\概览\字段\博客\概览\值,
节点\数据\字段\博客\概览。字段\博客\概览格式为节点\数据\字段\博客\概览\字段\博客\概览\格式,
term_data.name作为term_data_name,
term_data.vid作为term_data_vid,
users.name作为users\u name,
users.uid作为用户\u uid,
node.created为node_created,
节点\数据\字段\博客\概述。字段\提要\作者\姓名\值作为节点\数据\字段\博客\概述\字段\提要\作者\姓名\值,
node.sticky作为node\u sticky
从节点
node.vid上的左连接项\节点项\节点=项\节点.vid
在term_node.tid=term_data.tid上左连接term_data term_data
左连接内容\字段\图像节点\节点上的数据\字段\图像.vid=节点\数据\字段\图像.vid
左连接内容\u类型\u博客节点\u数据\u字段\u博客\u node.vid上的概述=节点\u数据\u字段\u博客\u概述.vid
节点上的内部联接用户。uid=users.uid
其中(node.type in('blog'))和(node.status 0)
按术语分组\u数据\u名称按节点排序\u已创建
限制5

为什么我不能使用上面的查询获取每个类别的最新消息?

我认为您的问题始于连接类型及其顺序。我建议您在对数据进行分组时,将group by fields表用作FROM表中的

为了简化,请从获取每个类别的最新文章的最新时间开始:

SELECT term_data.name, MAX(node.node_created) AS mostrecent
FROM term_data
JOIN term_node
  ON term_node.tid = term_data.tid
JOIN node node 
 ON node.vid = term_node.vid
WHERE (node.type in ('blog')) AND (node.status <> 0)
GROUP BY term_data.name

你得到了什么?我得到了过时的消息我得到了与desc和ASC相同的输出我的建议是阅读关于group by的文章。您没有对GROUP BY使用聚合函数,也没有按选择列表中的所有列进行分组,这意味着服务器可以从每个组中自由选择任何值。ORDER BY不能保证正确的结果。我已经在创建的节点上应用了max,但它仍然不起作用
SELECT {your field list}
FROM [insert above query] AS latest_news
JOIN term_data
  ON term_data.name = latest_news.name
JOIN term_node
  ON term_node.tid = term_data.tid
JOIN node node 
  ON node.vid = term_node.vid
    AND latest_news.mostrecent = node.node_created
JOIN users users
  ON node.uid = users.uid
LEFT JOIN content_field_image node_data_field_image 
  ON node.vid = node_data_field_image.vid
LEFT JOIN content_type_blog node_data_field_blog_overview 
  ON node.vid = node_data_field_blog_overview.vid