2011-05-19 74 views
0

我有一個表的字段(cat_tags),它包含以下行: -鮮明表字段標識

id - cat_tags - date_added 
    1 - yam, potato, onion, pepper,beans - 23-12-2011 
    row1 - yam, potato, onion, pepper 23-12-2011 
    row2 - onion, pepper, beans - 23-12-2011 
    row3 - pepper, beans, rice - 23-12-2011 
    row4 - rice, potato, pepper, yam - 23-12-2011 
    row5 - beans, rice, onion, food - 23-12-2011 
    ..... 
    ..... 
    ..... 

請我怎麼能得到項目的DISTINCT列表和次數(頻率),他們發生在整個表格字段?

例如,胡椒×4
洋蔥×3
山藥×2

預先感謝您

+0

什麼是表格結構?這些值是存儲在單個列中還是存儲在單獨的列中? – 2011-05-19 13:24:05

+0

你是說「山藥,土豆,洋蔥,胡椒」都出現在一個領域?或者逗號表示新的字段? – Dan 2011-05-19 13:26:22

+0

它們都在一個列中。用逗號分隔。 – 2011-05-19 13:30:23

回答

1

如果我理解正確你的問題,這應該工作:

$result = mysql_query("SELECT cat_tags FROM table"); 

    while($row = mysql_fetch_array($result)) 
    { 
     //Split the row into an array based on the comma 
     $detail_array = explode(",", $row['cat_tags']); 

     //Go through that array and index a master count for each occurence of the same value 
     //Trim due to the spaces after the comma in your field 
     foreach($detail_array as $key=>$val) 
     { 
      $output_array[trim($val)] = $output_array[trim($val)] + 1; 
     } 
    } 

    print_r($output_array); 
+0

如何格式化print_r($ output_array)以使其更具可讀性? – 2011-05-19 13:50:28

+0

@Frank,你可以用你想要的任何方式輸出數組。如果你是PHP的新手,你可以從這裏的一些文檔開始http://www.php.net – 2011-05-19 14:02:27

+0

如何?我知道php.net手冊存在... – 2011-05-19 14:07:39

1

你首先應該normalize你的表結構。 它不符合第一範式,使請求的操作方式比需要的複雜。

+0

你能舉個例子嗎? – 2011-05-19 13:34:09

3

首先規範化表(一個或多個)。

Table tags 
    id unsigned integer autoincrement primary key, 
    tag varchar(40) not null, 
    unique index tag(tag) 

Table Item_tags 
    id unsigned integer autoincrement primary key, 
    tag_id integer not null, 
    item_id integer not null 

Table items 
    id unsigned integer autoincrement primary key, 
    date_added date not null 

使用下面的一系列查詢插入一個項目到你的數據庫:

INSERT INTO Items VALUES (null, NOW()); 
SELECT @last_item_id:= LAST_INSERT_ID(); 

REPLACE IGNORE INTO tags (tag) VALUES ('pepper', 'onion', 'rice') 

INSERT INTO Item_tags 
    SELECT 
    null as id 
    , tags.id 
    , last_item_id 
    FROM tags WHERE tags.tag IN ('pepper', 'onion', 'rice') 

如果你想簡化添加對象,並簡化你的PHP代碼,
使用黑洞表並在該表上觸發。

CREATE TABLE bh_items (
    id unsigned integer autoincrement primary key, 
    date_added timestamp, 
    tag1 varchar(45) not null, 
    tag2 varchar(45) default null, 
    tag3 varchar(45) default null, 
    tag4 varchar(45) default null, 
    tag5 varchar(45) default null, 
    tag6 varchar(45) default null, 
    tag7 varchar(45) default null, 
    tag8 varchar(45) default null, 
    tag9 varchar(45) default null, 
    tag10 varchar(45) default null) ENGINE = blackhole; 

DELIMITER $$ 

CREATE TRIGGER ai_bn_items_each AFTER INSERT ON bh_items FOR EACH ROW 
BEGIN 
    DECLARE last_item_id integer; 

    INSERT INTO Items VALUES (null, new.date_added); 
    SELECT LAST_INSERT_ID() INTO last_item_id; 

    REPLACE IGNORE INTO tags ((null, new.tag1) 
          ,(null, new.tag2) 
          ,(null, new.tag3) 
          ,(null, new.tag4) 
          ,(null, new.tag5) 
          ,(null, new.tag6) 
          ,(null, new.tag7) 
          ,(null, new.tag8) 
          ,(null, new.tag9) 
          ,(null, new.tag10)); 

    INSERT IGNORE INTO item_tags (item_id, tag_id) 
     SELECT last_item_id, tags.tag FROM tags 
     WHERE tags.tag 
     IN (new.tag1, new.tag2, new.tag3, new.tag4, new,tag5 
      , new.tag6, new.tag7, new.tag8, new.tag9, new.tag10); 
END $$ 

DELIMITER ; 

如果您使用觸發你可以插入到黑洞表,然後觸發將更新所有3個表。

INSERT INTO bh_items VALUES (null, null, 'onion', 'rice', 'pepper' 
          , null, null, null, null, null, null, null) 

您的所有表格將自動更新。

SELECT tags.tag, COUNT(*) as freq FROM item_tags 
INNER JOIN tags ON (item_tags.tag_id = tags.id) 
GROUP BY item_tags.tag_id 

鏈接:

回到你的問題

你可以從這個使用這個查詢,然後選擇
黑洞:http://dev.mysql.com/doc/refman/5.1/en/blackhole-storage-engine.html
更換成:http://dev.mysql.com/doc/refman/5.1/en/replace.html
觸發器: http://dev.mysql.com/doc/refman/5.1/en/triggers.html
創建表格:http://dev.mysql.com/doc/refman/5.1/en/create-table.html