2012-05-11 89 views
2

我有這兩個表中的一些數據樣本。我想抽出每個類別的分類數量。我試了一下,每一個都不正確,我得到了(2)。所以希望有人能幫助我。計數功能()

CREATE TABLE IF NOT EXISTS `categories` (
    `id` int(255) NOT NULL AUTO_INCREMENT, 
    `name` text COLLATE utf8_unicode_ci NOT NULL, 
    `subcategory_id` int(2) NOT NULL DEFAULT '0', 
    `parent_id` int(255) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=27 ; 

-- 
-- Dumping data for table `categories` 
-- 

INSERT INTO `categories` (`id`, `name`, `subcategory_id`, `parent_id`) VALUES 
(1, 'Announcements', 0, 0), 
(2, 'Employment', 0, 0), 
(3, 'Items For Sale', 0, 0), 
(4, 'Services', 0, 0), 
(5, 'Garage Sales', 0, 0), 
(6, 'Automobiles', 0, 0), 
(7, 'Announcement1', 1, 1), 
(8, 'Announcement2', 1, 1), 

-- 
-- Table structure for table `classifieds` 
-- 

CREATE TABLE IF NOT EXISTS `classifieds` (
    `classified_id` int(255) NOT NULL AUTO_INCREMENT, 
    `title` text COLLATE utf8_unicode_ci NOT NULL, 
    `description` text COLLATE utf8_unicode_ci NOT NULL, 
    `category_id` int(10) NOT NULL, 
    `name` text COLLATE utf8_unicode_ci NOT NULL, 
    `authorized` int(10) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`adid`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=177 ; 

-- 
-- Dumping data for table `classifieds` 
-- 

INSERT INTO `classifieds` (`classified_id`, `title`, `description`, `category_id`, `name`, `authorized`) VALUES 
(1, 'Test Classified', 'Here is the First Test classified listing.', 1, 1); 

INSERT INTO `classifieds` (`classified_id`, `title`, `description`, `category_id`, `name`, `authorized`) VALUES 
(2, 'GMC For Sell', 'Looks like new 1979 GMC.', 6, 1); 

這裏

$query = "SELECT category_id, COUNT(title) FROM classifieds GROUP BY category_id"; 
$result = mysql_query($query) or die(mysql_error()); 
$row = mysql_fetch_array($result) 
$num_items_in_category = $row['COUNT(title)']; 
echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>"; 

感謝

+1

首先,你應該給的別名'COUNT(標題)'。第二:'$ row ['id']'和'$ row ['name']'從哪裏來? –

+0

我試過這樣:$ query =「SELECT category_id,COUNT(title)as'total'FROM classifieds GROUP BY category_id」;但沒有運氣 – Rocks

+2

附註:您的'INSERT INTO classifieds'語句沒有匹配的參數數量,看起來像'name'缺失。 – Kaivosukeltaja

回答

2

更改SQL一點,遍歷的結果?

$query = "SELECT c.id, c.name, 
       COUNT(cl.category_id) AS num_items_in_category 
       FROM category_id c 
       LEFT JOIN aclassifieds cl ON cl.category_id=c.id 
       GROUP BY c.id"; 
$result = mysql_query($query) or die(mysql_error()); 

while ($row = mysql_fetch_array($result)) { 
    echo "<li><a href='category-".$row['id'].".php' >".$row['name'].$row['num_items_in_category']."</."</a></li>"; 
} 
+0

我感謝您的幫助,但我沒有一個名爲category_id或aclassifieds的表。 – Rocks

+0

如果您將'category_id'更改爲'categories'和'aclassifieds'''classifieds',那該怎麼辦? –

+1

改變了一下,它的作品完美:)感謝你們所有人 – Rocks

0

萬一有人想以讓我的這個:

enter $query = "SELECT c.id, c.name, 
      COUNT(cl.title) AS num_items_in_category 
      FROM categories c 
      LEFT JOIN classifieds cl ON cl.category_id=c.id 
      GROUP BY c.id"; 
    $result = mysql_query($query) or die(mysql_error()); 

而($行= mysql_fetch_array($結果)){ 回聲 「

  • 」 $行[ '名稱']。 。$ row ['num_items_in_category']。「
  • 」; } 這裏 感謝