-1

I have two tables Userdb and Approval. Both tables have a common column but different names (LanID & UserID). In the Userdb, there are columns LanID, Name, DepartmentName.

I need to show these records :

  1. His own records
  2. All records in the same department

My current code:

SELECT 
    Userdb.LanID, Approval.UserID
FROM 
    Userdb_table, Approval_table  
JOINS
    Approval_table ON Userdb.LanID = Approval.UserID

My current code does not fulfill the second condition, how can I do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Angel Lim
  • 27
  • 1
  • 6
  • use conditions along with joins. – Vishwa Ratna Dec 31 '18 at 03:39
  • 3
    Sample Data and output will help ! – Avi Dec 31 '18 at 03:51
  • I agree with Avi, some toy data examples would help. But to show all records in the *same* department? I would say borrow @Guarav's example and add this condition: SELECT ut.LanID, ut.Name, ut.DepartmentName FROM Userdb_table ut INNER JOIN Approval_table at ON ut.LanID = at.UserID WHERE ut.DepartmentName = 'A' – jgreve Dec 31 '18 at 04:08

1 Answers1

1

Please note the way you are joining the two tables(comma separated join) is not recommended and should be avoided. You can use below query to get the result. I am assuming that you want these three columns in select list to be displayed, if you want something else from Approval_table also you can add those in select statement. I hope it will help.

  SELECT ut.LanID, ut.Name, ut.DepartmentName
  FROM Userdb_table ut INNER JOIN Approval_table at
  ON ut.LanID = at.UserID
Gaurav
  • 623
  • 5
  • 11
  • @Angel Lim, if you're wondering *why* the older comma separated style isn't recommended, this is worth a look: https://stackoverflow.com/a/1018877/5590742 (tldr: INNER JOIN is more human readable) – jgreve Dec 31 '18 at 04:01