MySQL - count different fields on different conditions in one joined table?

SELECT some_column, (SELECT COUNT(1) FROM Table1 WHERE mt. Id = joinable_key AND condition1 = 'something' ) AS first_condition, (SELECT COUNT(1) FROM Table1 WHERE mt. Id = joinable_key AND condition2 = 'something else' ) AS second_condition FROM MainTable mt.

This subquery solution was the proper one, thank you! – Swader Apr 23 at 16:34.

SELECT Main_Table. Some_column, SUM(t1. Condition1 = 'something') AS first_condition, SUM(t1.

Condition2 = 'something_else') AS second_condition FROM Main_Table LEFT JOIN Table1 AS t1 ON t1. Joinable_key = Main_Table. Id AND ( t1.

Condition1 = 'something' OR t1. Condition2 = 'something else' ) GROUP BY Main_Table. Id If both condition1 and condition2 are selective, this syntax may be more efficient: SELECT Main_Table.

Some_column, ( SELECT COUNT(*) FROM table1 t1 WHERE t1. Joinable_key = main_table. Id AND t1.

Condition1 = 'something' ), ( SELECT COUNT(*) FROM table1 t1 WHERE t1. Joinable_key = main_table. Id AND t1.

Condition2 = 'something_else' ) FROM Main_Table You should create the following indexes: table1 (id, condition1) table1 (id, condition2) for this to work fast.

I apologize for not responding sooner. I will do my best to test this properly as soon as possible. – Swader Nov 13 '10 at 16:01.

I tried both of these ideas and the result was SomeColumn | Something | SomethingElse -------------------------------------- 1 |24 |300 . |24 |300 . |24 |300 300 |24 |300 I had to put LIMIT 1 on the end of the query to show only one result.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions