Jump to content

SELECT - need to get row with a 0...


rab

Recommended Posts

I am trying to make my MySQL query to select all the columns in the news table and the number of comments for it. The query I have will only return the news rows with comments.

I know the "WHERE (news.id = comments.news_id)" is preventing it but i dont know how to just fill the column with a 0 if there is none.

Query
[code]
SELECT news.*, comments.news_id, COUNT(comments.news_id) as num_comments FROM news, comments WHERE (news.id = comments.news_id)  GROUP BY news.id
[/code]

Tables
[code]
CREATE TABLE `news` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(100) collate latin1_general_ci NOT NULL,
  `date` date NOT NULL,
  `user` varchar(50) collate latin1_general_ci NOT NULL,
  `news` varchar(500) collate latin1_general_ci NOT NULL,
  `category` varchar(100) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ;

CREATE TABLE `comments` (
  `id` int(11) NOT NULL auto_increment,
  `news_id` int(11) NOT NULL,
  `user` varchar(50) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34633-select-need-to-get-row-with-a-0/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.