Deepak Setia
Deepak Setia

Reputation: 45

How to get data from different tables into a singe table

I am working on a project in which i need to show some updates as like facebook(latest update on 1st number or you can say in LIFO manner) . I am fetching records from different tables ... I have 3 kinds of user queries : 1st is user's Company Projects, 2nd is Companies Order Queries , 3rd is Vendor's replies to company Order queries.

My SQL CODE is :

--user's Company Projects Table Query

 SELECT * FROM COMPANY_PROJECTS WHERE REF_COMPANYID=1003

-- Companies Order Queries Table Query

SELECT * FROM QUERYRECORDFROMUSER QR
  LEFT JOIN COMPANY_MASTER CM ON CM.COMPANY_ID = QR.QUERYFROM
  WHERE   cm.COMPANY_ID = 1003
  order by P_QUERYID desc

--Vendor's replies to company Order queriesTable Query 
  select  QUOTE_ID,NAME,P_QUERYID,ENQUIRYTITLE,REMARKS from QUOTE_FOR_ENQUIRY qe
  left join QUERYRECORDFROMUSER qr
  on qr.P_QUERYID = qe.REF_QUERYID
  left join VENDOR_REGISTRATION vr 
  on vr.VENDOR_REGID = qe.REF_VENDORID
  where REF_COMPANYID=1003

order by QUOTE_ID descenter image description here

I am having my data from 3 queries but i cant sort it according to Create Date .. because i m binding it in three repeaters in front end and it will show table 1, then table 2 , and then table 3 records but i want to show as per created date no matter record is coming form table 1, 2, or 3.

Thanks in Adavnce !!

Upvotes: 0

Views: 101

Answers (1)

NiH
NiH

Reputation: 466

If your tables would look the same (i.e. identical data types etc.), you could use UNION. However, this is not the case, which makes it somewhat more tricky. One solution would be to add NULL values in the columns that are missing. So if you have for example the following tables with respective columns:

table A: a, b, c

table B: a', b, d, e

table C: a'', b, f, g, h

where column a, a' and a'' are your IDs which are integers and b is your create date, you could do something like:

SELECT a, b, c, NULL, NULL, NULL, NULL, NULL FROM A
UNION  a', b, NULL, d, e, NULL, NULL, NULL FROM B
UNION  a'', b, NULL, NULL, NULL, f, g, h FROM C
ORDER BY b DESC

Upvotes: 2

Related Questions