mysql - how to count the two count function's return value in once sql query -
i have 3 tables a,b,c.their relation a.id b's foreign key , b.id c's foreign key.i need sum value when b.id = c.id , a.id = b.id ,i can count number query twice. need way count summation once time !
my inefficient solution
select count(c.id) c,b c.id = b.id; //return value x select count(a.id) c,b a.id = b.id; //return value y select x + y; // count summation fo x , y
how can optimize ? thks! :)
ps:
my question galaxql,which sql interactive tutorial.i have abstract problem,more detail can check section 17.select...group by... having...
you can these things in 1 query. instance, this:
select (select count(*) c join b on c.id = b.id) + (select count(*) c join on c.id = a.id)
(your second query not parse because a
not recognized table alias.)
in case, if learning sql, first thing should learn modern join
syntax. implicit joins using out of date 15 years ago , have been part of ansi standard on 20 years. learn proper join
syntax.
Comments
Post a Comment