Struggling with simple query

Discussion in 'Databases' started by neha2011, May 30, 2011.

  1. #1
    Hi guys,

    I have this table schema:

    id | type | lang
    ------------------------
    01 | article | en
    02 | article | ja
    03 | blog | en
    04 | blog | fr

    And am trying to generate this report:

    type | lang | count
    ------------------------
    article | en | 100
    article | ja | 200
    blog | en | 300
    blog | fr | 200

    I thought it would be simple but it's proving to be beyond my skill set.
    Any help you could offer would be appreciated.
     
    neha2011, May 30, 2011 IP
  2. friendlymentor

    friendlymentor Peon

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT type,lang,count FROM table_name
     
    friendlymentor, May 30, 2011 IP
  3. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #3
    Wrong. Here is the right way, this is untested but should work.

    
    
    $sql = mysql_query('SELECT `type`,`lang`,count(*) FROM `TABLENAMEHERE` GROUP BY `lang`,`type`') or die(mysql_error());
    
    if (mysql_num_rows($sql)) {
    
    	die('no results');
    
    } else {
    
    	while($r = mysql_fetch_row($sql)) {
    
    		$type = stripslashes($r[0]);
    
    		$lang = stripslashes($r[1]);
    
    		$count = $r[2];
    
    		/* put something here to perform or display. */
    
    	}
    
    }
    
    
    PHP:
     
    Last edited: May 30, 2011
    DomainerHelper, May 30, 2011 IP