• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

Select Data From Two Tables And show distinct records and second table records come first then first table record and having relationship.

Suppose i have table name tblfavuser(Contains data of favorite user)    and tbluser(contains the user data). I  want to show distinct record from both table and tblfavuser comes first then tbluser.
0
Gurbirs
Asked:
Gurbirs
1 Solution
 
Muhammad Noman IqbalCommented:
Hi,

You can use Union

Select  fav_user.id, favuser.name from fav_user
union
Select user.id, user.name from user

0
 
awking00Commented:
Can you post some sample data and the expected results?
0
 
MarkusIdCommented:

col ordertype noprint
 
SELECT 1 ordertype, tblfavuser.*
  FROM tblfavuser
  WHERE not exists (select 1
                                    from tbluser
                                   where tbluser.id = tblfavuser.id)
UNION
SELECT 2, tbluser.*
   FROM tbluser
 WHERE not exists (select 1
                                    from tblfavuser
                                   where tblfavuser.id = tbluser.id)
order by 1

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now