I have 3 table, Documents
, SR
and events
In documents
I am saving all documents related to SR and Events.
And I want to show all documents in one page.
So I am using this select query
Select *
from documents d, SR s, Events e
where d.relationid = ( case d.documenttype when 'SR' the s.SRId else 'e.eventid end)
but it's not working.
My document
table structure is like this :
documentid int,
documenttype nvarchar(50),
relationid int,
document image,
docdate date
Can anyone please point me to my mistake ?
I want to select all documents with related info. means if its SR document than SR details should display otherwise Events. There is only 2 types of documents right now.
What should be my select query for this ?
You can join then using
LEFT JOIN
,where
Col1,...., Col4
are the columns of each table youw ant to be displayed based ondocumenttype
.To further gain more knowledge about joins, kindly visit the link below:
The more safe version of the query above assuming that the same ID can contain on
SR
andEvents
table would be by usingCASE()
You no need to write Where clause as you need all records
Below Query Works for sure
This should work.
Something along these lines