SQL查询,每个特定列限制为1个结果

时间:2013-05-07 10:27:01

标签: mysql sql

根据下面的查询,我可以得到一个结果集,其中包含查询中列出的位置ID的所有新故事,并按日期排序。我不明白的是如何重写查询,使其限制为每个位置ID的1个结果(最新)。

是否可以这样做,或者我之后应该只处理结果集?

提前致谢,

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
FROM 
{node} node
INNER JOIN {field_data_field_story_location} field_data_field_story_location 
ON node.nid = field_data_field_story_location.entity_id 
AND (field_data_field_story_location.entity_type = 'node'
WHERE (( (node.status = '1') AND (node.type IN  ('_news_story')) 
AND (field_data_field_story_location.field_story_location_tid IN  
('38', '44', '46', '45', '30', '36', '37', '29', '33', '34', '28', '56', '21', '49', '32')) ))
ORDER BY node_created DESC

1 个答案:

答案 0 :(得分:2)

您可以在之后处理结果集,但在数据量很大的情况下效率不高。

为了提高效率,您可以使用限制1编写n个查询(每个field_story_location_tid一个),然后使用UNION sintax

加入结果集