我有一个表“文章”,有一列“公司”,其中有公司名单或文章类型 - 蹩脚我知道,但它不是我的数据库。 :)假设有 n每种类型的文章。我需要选择属于该类型的第一篇文章(基于年份或任何其他标准)。就像这样:通过字符串和限制MySQL查询
select * from details where (company = 'aaa' or company = 'bbb' or ...)
我知道类型是什么,所以他们可以硬编码。我只需要限制每种类型的第一篇文章。谢谢!
EDIT
给定的采样数据:
id company copy issue ------------------------ 1 apple 'abc' NULL 2 bmw 'abc' NULL 3 ibm 'abc' NULL 4 news 'abc' 2 5 news 'abc' 3 6 seagate 'abc' NULL 7 events 'abc' 1 8 features 'abc' 5 9 samsung 'abc' NULL
我需要的行4,7,8。
EDIT2
对不起,如果我不清楚。本质上,表格包含两种不同类型的数据。一个是公司信息,一个是文章信息。基本上我需要这样做:
select * from articles where company = "news" order by issue limit 1;
select * from articles where company = "events" order by issue limit 1;
select * from articles where company = "features" order by issue limit 1;
但只有一个查询。
这确实取决于标准。 MySQL很难在单个查询中仅选择组中第一个匹配行的字段。 – gahooa 2012-03-26 03:29:25
您的可排序数据在哪里? #5之前排第4排的是什么?另外,我看不出#7和#8有什么区别。为什么不#1,#2,#3等? – Phil 2012-03-26 03:32:01
这就是数据输入的方式,id是一个自动增量。这比这更复杂一点,但为了方便起见,让我们假设它只是id不同。 – Alex 2012-03-26 03:35:21