"Vaidas Zilionis aka Why2liz" <[EMAIL PROTECTED]> wrote:
> I had wroted query
> 
> SELECT r.`id`, COUNT(s.`id`) AS `sk` FROM `sodybos` as s RIGHT OUTER JOIN
> `rajonai` as r ON (s.`rajonai_id` = r.`id`) GROUP BY r.`id`
> 
> But this query run's slow, now I want to optimize it, maybe write it without
> JOIN command
> 
> If I wrote new query (it's more faster)
> 
> select r.id, count(s.id) as sk from rajonai as r, sodybos as s WHERE
> s.rajonai_id = r.id group by r.id
> 
> It's work good, but I can't get rows which count(s.id) = 0
> 
> Example with 1 query I get
> 
> 1|12
> 2|0
> 3|15
> 4|5
> 5|1
> 
> With second only
> 
> 1|12
> 3|15
> 4|5
> 5|1
> 
> How to get and 2 row? Which count result = 0

You got different results because you use different JOINs. In your case you need RIGHT 
or LEFT JOIN:
        http://www.mysql.com/doc/en/JOIN.html

How MySQL optimises RIGHT and LEFT JOIN is described at:
        http://www.mysql.com/doc/en/LEFT_JOIN_optimisation.html



-- 
For technical support contracts, goto https://order.mysql.com/?ref=ensita
This email is sponsored by Ensita.net http://www.ensita.net/
   __  ___     ___ ____  __
  /  |/  /_ __/ __/ __ \/ /    Egor Egorov
 / /|_/ / // /\ \/ /_/ / /__   [EMAIL PROTECTED]
/_/  /_/\_, /___/\___\_\___/   MySQL AB / Ensita.net
       <___/   www.mysql.com




-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to