I'm having a database here and I'm looking to set up a SQL to generate a dataset in Delphi and then populate a Treeview. The figure below shows part of the bank, but that is the general structure of it:
Basicallytheuserlogsintothesystem(tbuser)generatingatupleinthetbuser_loggertable,whichtakestheuserid,itthencreatesaprojectinthetbprojecttableandthengeneratesatupleinthetbdataidtable,whichgoestotheprojectidandtheloggedinuser.Aftereachothertablethattheuserwritesadataasthetablestbclient,tbcasa,tbcarro,tbam,thegeneratedidofthetuplegeneratedpreviouslyinthetbdataidtablewillberecorded,soIcanknowwhichprojectthatdatawasrecordedandwhichuserwhowasloggedinwhenthatdatawascreated.ThiswayIcanthenfilterforexamplethedatafromthetabletbcasabytheprojectthatisbeingusedatthattime.I'msettingupanSQLasfollows:
SELECTt1.datId,t2.instNome,t3.cltNome,t6.docNomeFROMtbdataidt1INNERJOINtbinstituicaot2ON(t1.datId=t2.instDat_Id)INNERJOINtbclientet3ON(t1.datId=t2.instDat_Id)INNERJOINtbdocumentot6ON(t1.datId=t6.docDat_Id)INNERJOINtbuser_loggert4ON(t1.datUsrLog_Id=t4.usrLogId)INNERJOINtbusert5ON(t4.usr_Id=t5.usrId)WHEREt1.datProj_Id=12#ondeoiddoprojetoé12
IntheaboveexampleIputonly3tables,butdoesnotbringanydata,ifyouusetheleftjoin,itbringsahalfcrazyquery.HowcanImountthisqueryforthistablestructureandlaterIcanmountTreeview(whichiseasier)?
EDITION
Iwasabletomountthequerywiththehelpbelow,itlookslikethis:
SELECTt1.datId,t2.projNome'Projeto',t6.usrLogin'Usuário',t3.instNome'Instituições',t4.cltNome'Clientes',t7.atvNome'Atividades',t8.atvGrpNome'GruposdeAtividades',t9.locNome'Locais',t11.locTipNome'TiposdeLocais',t12.medNome'EstaçõesdeMedição'FROMtbdataidt1LEFTJOINtbprojetot2ON(t1.datId=t2.projDat_Id)LEFTJOINtbinstituicaot3ON(t1.datId=t3.instDat_Id)LEFTJOINtbclientet4ON(t1.datId=t4.cltDat_Id)LEFTJOINtbativt7ON(t1.datId=t7.atvDat_Id)LEFTJOINtbativgrupot8ON(t1.datId=t8.atvGrpDat_Id)LEFTJOINtblocalt9ON(t1.datId=t9.locDat_Id)LEFTJOINtblocalativt10ON(t1.datId=t10.locAtvDat_Id)LEFTJOINtblocal_tipot11ON(t1.datId=t11.locTipDat_Id)LEFTJOINtbmedt12ON(t1.datId=t12.medDat_Id)LEFTJOINtbmedvart13ON(t1.datId=t13.medVarDat_Id)LEFTJOINtbuser_loggert5ON(t1.datUsrLog_Id=t5.usrLogId)LEFTJOINtbusert6ON(t5.usr_Id=t6.usrId)WHERE(t1.datProj_Id=1AND(t6.usrId=2ANDt1.datProjCen_IdISNULL))OR(t1.datProj_Id=1ANDt1.datProjCen_IdISNULL)OR(t1.datProj_IdISNULLANDt6.usrId=2)
TheresultlookssomethingliketheimagebelowandasIwantedit,stillbringstupleswhereeverythingisNULL.WhatIwantedtoknowishowcanIimprovethisquery?
When I register multiple tables with only one dataid value my query brings something like the image below, even though my database is too small.