sql - Show COUNT(*) column for value 0 in multiple rows -


i have table content

content(id, contenttext, contentdate, iduser → user, contenttype)

and table votequestion

votequestion(iduser → user, idquestion → content, isup)

i want select, instance, first 30 recent questions.

select *  "content"  "type" = 'question' order "contentdate" limit 30 

however, want concatenate columns information related question, don't need query database again each question returned.

for instance, want count votes each question, , returned in same row.

example:

| id | contenttext | contentdate | iduser | contenttype | votes | ----------------------------------------------------------------- | 2  |   'abc'     | '2013-03-25'|  192   |  'question' |  10   | 

i tried following query:

with question (select *  "content"  "type" = 'question' order "contentdate" limit 30 )  select count("votequestion"."iduser")  "votequestion", question "idquestion" = question."id" group question."id"; 

but doesn't return questions number of votes = 0 (only 16 questions returned instead of 30). how solve it?

then, how can concenate both tables? , how can subtract votes isup true , isup false in query?

you can subselect vote count:

select content.* ,  (   select count(*)    votequestion   idquestion = content.id ) votes content type = 'question' order contentdate limit 30 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -