2013-02-13 69 views
2

4類在一個表中的數據庫。
我想從數據庫中的每個類別獲取最常訪問的四篇文章。
這意味着得到(4 articles most visited from category1) + (4 articles most visited from category2) + (4 articles most visited from category3) + (4 articles most visited from category4) =合計16 articles4 categories我想從每個類別訪問最多的文章(4類)

,我想這樣做,通過一個查詢。

普通查詢:

$sql="select ID,name,country from article where `active`='yes' order by `visit` desc limit 16 "; 

在文章表結構:

CREATE TABLE `article` (
    `ID` int(11) NOT NULL auto_increment, 
    `name` varchar(255) NOT NULL default '', 
    `country` varchar(50) NOT NULL default '', 
    `town` varchar(30) NOT NULL default '', 
    `other_town` varchar(30) NOT NULL default '', 
    `title` varchar(255) NOT NULL default '0', 
    `size` varchar(30) NOT NULL default '', 
    `type` varchar(30) NOT NULL default '', 
    `tel` varchar(30) NOT NULL default '', 
    `mobile` varchar(30) NOT NULL default '', 
    `connect` varchar(30) NOT NULL default '', 
    `email` varchar(30) NOT NULL default '', 
    `photo1` varchar(100) NOT NULL default '', 
    `print` varchar(30) NOT NULL default '', 
    `small_pic1` varchar(100) NOT NULL default '', 
    `detail` text NOT NULL, 
    `add_by` int(11) NOT NULL default '0', 
    `cat` int(11) NOT NULL default '0', 
    `goods_type` enum('new','old') NOT NULL, 
    `add_date` date NOT NULL default '0000-00-00', 
    `end_date` date NOT NULL default '0000-00-00', 
    `period` varchar(30) NOT NULL default '', 
    `visit` int(11) NOT NULL default '0', 
    `comment` int(30) NOT NULL default '0', 
    `fav` varchar(15) NOT NULL default '', 
    `favorite` varchar(15) NOT NULL default '', 
    `active` varchar(15) NOT NULL default '', 
    `rate` int(11) NOT NULL default '0', 
    `short` text NOT NULL, 
    `add_to` varchar(50) NOT NULL default '', 
    `author` varchar(50) NOT NULL default '', 
    `author_img` varchar(50) NOT NULL default '', 
    `lang` varchar(10) NOT NULL default '', 
    `budget` varchar(50) NOT NULL default '', 
    PRIMARY KEY (`ID`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ; 

類別表結構:

CREATE TABLE `category` (
    `ID` int(11) NOT NULL auto_increment, 
    `name` varchar(255) NOT NULL default '', 
    `img` varchar(255) NOT NULL default '', 
    `sub` int(11) NOT NULL default '0', 
    `type` varchar(20) NOT NULL default '', 
    `lang` varchar(10) NOT NULL default '', 
    `active` varchar(25) NOT NULL default '', 
    `add_by` int(11) NOT NULL default '0', 
    PRIMARY KEY (`ID`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
+1

您可以提供您的表定義? – sgeddes 2013-02-13 15:22:46

+0

如果你能提供表格結構的例子,那就太好了。 – 2013-02-13 15:23:18

+0

以及迄今爲止您一直在嘗試的內容。 – 2013-02-13 15:23:57

回答

0

試試這個:

SELECT ID, NAME, country, catCnt 
FROM (SELECT ID, NAME, country, cat, IF(@cat=(@cat:=cat), @cnt:[email protected]+1, @cnt:=1) catCnt 
     FROM (SELECT ID, NAME, country, cat FROM article WHERE `active`='yes' ORDER BY cat, `visit` DESC) A, 
      (SELECT @cat := 0, @cnt:=1) B 
    ) A 
GROUP BY cat HAVING catCnt <= 4; 

OR

SELECT ID, NAME, country 
FROM (SELECT ID, NAME, country, cat, IF(@cat=(@cat:=cat), @cnt:[email protected]+1, @cnt:=1) catCnt 
     FROM (SELECT ID, NAME, country, cat FROM article WHERE `active`='yes' ORDER BY cat, `visit` DESC) A, 
      (SELECT @cat := 0, @cnt:=1) B 
    ) A 
WHERE catCnt <= 4; 
+0

錯誤信息:'每個派生表都必須有自己的別名' – 2013-02-13 16:09:18

+0

@LionKing立即嘗試 – 2013-02-13 16:10:04

+0

感謝您的嘗試:它正在工作,但不完全是我想要的。我想從每個分類中獲得最多訪問的4篇文章。您的查詢獲得4篇以上的文章,而不是排序。 – 2013-02-13 16:26:26

1

這是可以做到的,但它是不是100%直線前進。我從我之前閱讀過的一個SO問題收藏了這篇文章(將嘗試並找到/鏈接)。基本上,如果您希望爲每個查詢創建一個組,您可以使用ORDER BY和LIMIT,但要在一個查詢中執行所有查詢,請參閱詳細介紹此操作的鏈接文章。

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

對於後人,我將嘗試發佈基於他所提供的第一種方法,它可以在時間比例來分類的數量成倍增長的例子彙總查詢:

select ID, name, country, cat, visit 
from article 
where visit = (select max(visit) from article as a where a.cat = article.cat) 
or visit = (select max(visit) from article as a where a.cat = article.cat 
    and visit < (select max(visit) from article as a2 where a2.cat = article.cat)) 

or visit = (select max(visit) from article as a where a.cat = article.cat 
    and a.visit < (select max(a2.visit) from article as a2 where a2.cat = article.cat 
    and a2.visit < (select max(a3.visit) from article as a3 where a3.cat = article.cat))) 

or visit = (select max(visit) from article as a where a.cat = article.cat 
    and a.visit < (select max(a2.visit) from article as a2 where a2.cat = article.cat 
    and a2.visit < (select max(a3.visit) from article as a3 where a3.cat = article.cat 
    and a3.visit < (select max(a4.visit) from article as a4 where a4.cat = article.cat)))) 
order by cat, visit desc 

或者,如果你用出來通過訪問指定說明類,只是爲了

這將返回前四在每個類別中,但要注意每級查詢的嵌套深,你正在尋找去想要的順序,遵循喜如果你需要繼續深入下去,他會提供更好的選擇(他和我將其描述爲YUCK)。

SQL小提琴:http://www.sqlfiddle.com/#!2/e115f/26

而且爲後人,我的測試數據:

CREATE TABLE article (
ID int auto_increment primary key, 
name varchar(20), 
country varchar(30), 
cat int, 
visit int 
); 

INSERT INTO article (name, country, cat, visit) VALUES 
('Test1', 'Canada', 1, 7),('Test2', 'Canada', 1, 2),('Test3', 'Ireland', 1, 1), 
('Test5', 'Ireland', 1, 3),('Test6', 'Ireland', 1, 8),('Test7', 'India', 1, 9), 
('Test8', 'Canada', 2, 11),('Test9', 'Canada', 2, 13),('Test10', 'Ireland', 2, 6), 
('Test11', 'Ireland', 2, 5),('Test12', 'Ireland', 2, 1),('Test13', 'India', 3, 1), 
('Test14', 'India', 3, 9),('Test15', 'India', 3, 8),('Test16', 'India', 3, 54); 
+0

謝謝,但不幸的是,並沒有給我從每個類別訪問量最大,也沒有排序。 – 2013-02-13 16:47:37

+0

@LionKing它,看看小提琴。新增訪問,以便您可以明確地看到它。排序也只是一個order by子句,也添加了。 – Matthew 2013-02-13 16:51:24

1

這應該這樣做。基本上確定由訪問列每個類別行號,然後才能選擇4強。這是一個濃縮版,但應該得到跨越點(添加您需要返回的字段):

SELECT Id, Cat, Visit, Name 
FROM (
    SELECT 
     @curRow:=CASE WHEN @prevRow = A.cat THEN @curRow+1 ELSE 1 END AS rn, 
     A.Id, 
     A.Visit, 
     A.Cat, A.Name, 
     @prevRow:=A.cat AS clset 
    FROM (SELECT A.Id, C.Id as Cat, A.Visit, C.Name 
      FROM Articles A 
      JOIN Category C ON A.cat = C.id 
      ORDER BY A.Cat, A.Visit DESC 
     ) A 
     JOIN (SELECT @curRow:=0) r 
     JOIN (SELECT @prevRow:=0) r2 
) B 
    WHERE rn <= 4 

有些小提琴:http://sqlfiddle.com/#!2/b7260/1

好運。

+0

錯誤消息:表't9-fast.articles'不存在 – 2013-02-13 16:14:50

+0

@LionKing - 我在發佈代碼之前編寫了此代碼 - 相應地替換字段名稱和表名稱:)您沒有文章表格 - 你的文章...祝你好運! – sgeddes 2013-02-13 16:16:05