A
A
Alexander2015-10-13 18:54:02
MySQL
Alexander, 2015-10-13 18:54:02

How to correctly compose a query with a subquery?

Friends. Tell me, what's wrong? The query returns zeros..
You need to get the number of news for each category..

SELECT ct.`id`, (SELECT count(`id`) FROM dle_post ps WHERE ps.category regexp 'ct.`id`' && approve = '1') as allnews_ FROM dle_category ct

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Baydikov, 2015-10-13
@vbaydikov

try like this:

select 
  ct.id,
  count(1) as allnews_
from dle_category ct
left join dle_post ps on ps.id = ct.id
where true
  and ps.approve = '1'
group by ct.id

maybe another problem is that approve is not a string / varchar
and in general it can be done easier if you only display id, count(1) - it's all there in dle_post, sort of, isn't it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question