2012-03-26 64 views
-2

我有一個表「文章」,有一列「公司」,其中有公司名單或文章類型 - 蹩腳我知道,但它不是我的數據庫。 :)假設有 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; 

但只有一個查詢。

+0

這確實取決於標準。 MySQL很難在單個查詢中僅選擇組中第一個匹配行的字段。 – gahooa 2012-03-26 03:29:25

+0

您的可排序數據在哪裏? #5之前排第4排的是什麼?另外,我看不出#7和#8有什麼區別。爲什麼不#1,#2,#3等? – Phil 2012-03-26 03:32:01

+0

這就是數據輸入的方式,id是一個自動增量。這比這更復雜一點,但爲了方便起見,讓我們假設它只是id不同。 – Alex 2012-03-26 03:35:21

回答

3

像這樣的事情也許

select * from details d 
where company in ('news', 'events', 'features') 
and not exists (
    select 1 from details d_ 
    where d_.company = d.company 
    and d_.id < d.id -- provide your sortable criteria here 
) 

例在這裏 - http://sqlfiddle.com/#!2/bb8f2/6

+0

這將工作,但公司是混合類型col:它可以表示公司或文章類型。 – Alex 2012-03-26 03:31:47

+0

@Alex我已將Mosty的答案添加到SQLFiddle中,以便您可以比較兩者。 – Phil 2012-03-26 03:53:16

+0

隨着一些玩耍,這似乎是伎倆。謝謝! – Alex 2012-03-26 04:07:49

0

假設 「公司」 和 「publication_date」 在表中 「詳細信息」 欄,然後是這樣的:

select * 
from details 
where company in ('aaa', 'bbb', ...) 
and publication_date between '2012-02-01' and '2012-03-01' 
order by publication_date desc 
limit 1 
+0

IN子句正是我所需要的,但我也需要將每種類型限制爲僅第一種。所以如果查詢返回4'aaa'和7'bbb'我只需要每個的第一個。 – Alex 2012-03-26 03:33:43

3

此查詢:

select t1.* from t t1 
left join t t2 
on t1.company = t2.company and t1.id > t2.id 
where t2.id is null and t1.company in ('news', 'events', 'features') 

將返回:

 
+----+----------+------+ 
| ID | COMPANY | COPY | 
+----+----------+------+ 
| 4 | news  | abc | 
| 7 | events | abc | 
| 8 | features | abc | 
+----+----------+------+ 

這就是你想要的?

注意:當你說the first article我假設的順序由ID領域提供

你可以用它玩here

編輯:

您的編輯後,查詢幾乎是同樣,只是將訂購字段更改爲issue而不是id

select t1.* from t t1 
left join t t2 
on t1.company = t2.company and t1.issue > t2.issue 
where t2.id is null and t1.company in ('news', 'events', 'features') 
+0

對不起,使事情變得複雜:它可能是id,也可能是日期,或雜誌問題還沒有得到方向......什麼是你的查詢中的「t」,「t1」,「t2」? – Alex 2012-03-26 03:40:58

+0

't'是表名。 't1'和't2'是表別名。我不明白你對不同順序的看法。我提供的解決方案會導致您的預期結果。你能否更新你的示例數據來顯示你的實際數據,看看爲什麼這不起作用?謝謝 – 2012-03-26 03:43:46

0

您正在嘗試執行group by
接下來要order by問題列。
然後你只需要第一行。

我用組連接創建一個列表,
然後提取有序列表group_concat(copy order by copy)中的第一個字符串。

select 
    id, 
    company, 
    substring(
    group_concat(copy order by copy), 
    1, 
    case when substring_index(group_concat(copy order by copy),',',1) > 1 
      then substring_index(group_concat(copy order by copy),',',1)-1 
     else 1 end 
    ) as copy 
from details d 
where company in ('news', 'events', 'features') 
group by company 

*名單group_concat(copy order by copy)不能太長,雖然,這取決於你的數據。