1086

MySQL 5.1 Problema

Salut la toti ! Ma chinui cu asa o problema toata ziua si nu gasesc nici o solutie.Am o baza de date cu urmatoarea structura---------------------------------------------------------------------------------[db_main] [C_Operation] [C_Office] [C_OfficeTIP]ID ID ID IDDATA NUME NUME NUMEOperation_ID TIP_IDOffice_ID-----------------------------------------------------------------------------------Relatiile sunt urmatoarele:Operation_ID -> C_Operation.IDOffice_ID -> C_Office.IDC_Office.TIP -> C_OfficeTIP.ID-----------------------------------------------------------------------------------Cum sa selectez toate inregistrarile din [C_Operation] pentru o perioada anumitadin baza [db_main] la care Office_ID este NULL sau TIP_ID=XExemplu : Avem clasificatorii[C_Operation] ID , NUME 1 AAAAA 2 BBBBB 3 CCCCC 4 DDDDD [C_OFFICE] ID , NUME TIP_ID 1 Office_1 12 Office_2 13 Office_3 24 Office_4 2[C_OfficeTIP] ID, NUME1 Tip_12 Tip_2si baza de date [db_main] ID, DATA , OPERATION_ID , Office_ID 1 01.01.2010 1 1 2 02.01.2010 4 4 3 02.01.2010 1 3 4 02.01.2010 2 NULL 5 04.01.2010 1 2 6 05.01.2010 1 1 7 07.01.2010 3 2 8 10.01.2010 1 3 9 10.01.2010 2 NULLCum sa obtin numarul de aparitii a fiecarei inregistrari din [C_Operation] in baza de date [db_main] din perioada [ 02-01-2010 - 05.01.2010 ] si la care Office_ID este NULL sau daca nu este NULL atunci C_OFFICE.TIP_ID = 1.In rezultat trebuie sa obtin tabelul: AAAAA = 2 BBBBB = 1 CCCCC = 0 DDDDD = 0===============================================Am incercat asa dar nu obtin rezultattul doritSELECT a.ID, a.NUME, COUNT( b.Operation_ID ) as NumarFROM c_operation a LEFT JOIN db_main bON (( b.Operation_ID = a.ID ) AND ( b.DATA BETWEEN '2010-01-02' AND '2010-01-05' ))LEFT JOIN c_Office c ON ( b.Office_ID = c.ID ) AND (( b.Office_ID is NULL) OR ( c.TIP_ID = 1))GROUP BY a.ID, a.NUME ORDER BY a.NUMEObtin asa tabel : AAAAA = 3 BBBBB = 1 CCCCC = 0 DDDDD = 1Da-ti care sfaturi ( solutii )Cer scuze obtin asa tabelAAAAA = 3BBBBB =0CCCCC =0DDDDD= 1-------------------------------------------------Am mai incercat si asa dar nu se ia in calcul cimpurile cu NULLSELECT a.ID, a.NUME, COUNT( b.Operation_ID ) as NumarFROM c_Operation a LEFT JOIN ( db_main b JOIN c_office c ON (( b.Office_ID = c.ID ) AND ( C.TIP_ID=1))) ON ((( b.Operation_ID = a.ID ) OR ( b.Office_ID is NULL )) AND ( b.DATA BETWEEN '2010-01-01' AND '2010-01-05' ))GROUP BY a.ID, a.NUME ORDER BY a.NUMEsi obtin asa tabel:AAAAB=2BBBBB=0CCCC=0DDDD=0
0