-1

I have db table structure like that :

--
-- Table structure for table `table_submissions`
--

CREATE TABLE IF NOT EXISTS `table_submissions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Submission ID',
  `item_name` bigint(20) unsigned NOT NULL COMMENT 'Item Name',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0 = pending, 1 = approved, -1 = denied',
  PRIMARY KEY (`id`),
  KEY `item_name` (`item_name`),
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

php

$date_info = $db->fetchOne("SELECT * FROM table_submissions");

I need to code php for "Most Required Item" order by count item_name limit 10

This mean the most item_name which the visitor has been required it will be in the top then ++ 9

How i can do that please ?

4

1 回答 1

3

I believe you want this:

SELECT 
    item_name, 
    count(1) 
FROM 
    table_submissions 
GROUP BY 
    item_name 
ORDER BY 
    count(1) DESC 
LIMIT 10;

Note this does not take into account one user requesting the same item 1000 times verses 1000 users requesting an item once each. If that's important to you then you need a way to filter by user ID.

于 2013-03-21T22:44:27.567 回答