Click here to Skip to main content
15,861,172 members

Join two tables and count -statistics

ixashish asked:

Open original thread
Need to write Mysql query showing statistics,

Description:
Parent table 60 lac records contains unique records
Child table with 1 crore Contains multiple records
HTML
 Parent Table
 ID  Flag1 Flag2
 1    1      1
 2    NULL   1
 3    1      1 

Child Table
 ID acc Category
  1 101  Cate1
  1 102  Cate5
  2 103  Cate2
  2 104  Cate3
  3 105  Cate4
  3 106  Cate4 

One to many relationship between two tables.

I need to write group by queries to find categoriwise and flag based count in one query with MySQL Database
sample:
I have tried with following query but i dont want to write where condition for each category and Flag as Child table contains 30 categories and 12 Flags
SQL
select count(1) from Parent A,Child B where A.ID=B.ID AND B.Category='CATE1' and A.Flag1=1

Result expected
Category  Flag1_Count Flag2_Count
  Cate1       1           0 
  Cate2       0           1
  Cate3       1           1

Please help.
Tags: SQL, MySQL

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900