Selecting from four columns in different table

  • how can we select from TabLa A,Table B Table C and Table D

    Table 1 has columns A B C ( A is the foreign key that refrences D )

    Table 2 has columns D E F ( D is the primary key that refrences D in table 1 and M in table 4 )

    Table 3 has columns G H I J K L ( G is the primary key and it refrences M in table 4)

    Table 4 has columns M N O P ( M is the foreign key that refrences D and N refrences G in table 3)

    Now i have to perfrom an insert by checking what values are in table 1 and then check these values with values in table 2, then if condition is sattisfied insert in table 4 by checking what is the value of L in table 3,

    how can i perform this

  • You should look into using Joins on the multiple tables. The purpose of a Join is to combine query results from two or more related tables into one logical result set.

    Below is an example of Joins being used on 3 different tables. You can easily modify it to add another table since the underlying theory would still be the same. On the lines where the Joins are preformed, you will see how the query is matching columns from one table (Sales.CustID) to another matching column in another table (Customers.CustID).

    SELECT Sales.*, Customers.*, Parts.*, Tax.*

    FROM Sales

    INNER JOIN Customers ON Sales.CustID = Customers.CustID

    INNER JOIN Parts ON Parts.PartID = Sales.PartID

    INNER JOIN Tax ON Tax.TaxID = Customers.TaxID

    More reading: http://msdn.microsoft.com/en-us/library/aa196318%28SQL.80%29.aspx

  • Since selecting from multiple tables is just about the most basic function there is in a database, I think what you should probably do is take a class or read a book on the subject. Don't start out with such a specific question, get a basic feel for the subject by taking a class. It will help more.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • This is what i am trying to do

    a store procedure that will insert rows to the user deduction table. Salary grade is determined by checking the salary grade table to derive the employee's salary grade. Print " no deduc" for users with no deduction and verify that a user is eligible for a deduction based upon their salary grade.Once you know an employee's grade, then verify if the employee is eligible for the deduction via comparing it to the minimum and maximum salary grade that is stored in the deduction table

    this is what i am trying to accomplish

    USer table has sal,userno,deptno

    deductions name nvarchar(3) grd_min decimal (9,2) grd_maX decimal (9,2)

    user_deduc (deduc Nvarchar(3),emp NUMBER(4),bfr_or_af CHAR, deduc amount int )

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply