Rory Lester
Rory Lester

Reputation: 2918

SQL query multiple tables

I have 2 tables and I need to select the relative information, need some help with the query.

Table 2 has ID, MaskID columns.

Table 3 has MaskID, MaskName, Total

Assuming that I have an ID already given, how can I select the ID, MaskName, Total from the tables? How do I traverse though them?

Upvotes: 1

Views: 5697

Answers (6)

Hamed
Hamed

Reputation: 2168

you shoud use this query

select ID,MaskName,Total from Table1 Inner join Table2 on Table1.MaskID = Table2.MaskId where ID = "given value"

Upvotes: -1

darnir
darnir

Reputation: 5180

SELECT a.ID, b.MaskName, b.Total from 2 a INNER JOIN 3  b ON a.MaskID=b.MaskID WHERE ID='Given value'

This is a simple MySQl/T SQL/ PLSQL query. Just use an INNER JOIN on the two tables. A Join works by combining the two tables side by side. The INNER JOIN only outputs the result of the intersection of the two tables. That is, only those rows where the primary key and foreign key have a matching value.

In certain cases, you may want other rows outputted as well, for such cases look up LEFT JOIN, RIGHT JOIN and FULL JOIN.

Upvotes: 0

coms
coms

Reputation: 479

select ID, MaskName, Total from TABLE_2
inner join TABLE_3 on (TABLE_2.MaskID=TABLE_3.MaskID)
where ID=111

Upvotes: 0

D. Dubya
D. Dubya

Reputation: 189

The TSQL query would be:

SELECT t2.ID, t3.MaskName, t3.Total
FROM Table2 AS t2 INNER JOIN Table3 AS t3 ON (t2.MaskId = t3.MaskId)
WHERE ID = 123

Unsure what you mean by 'traverse' through them.

Upvotes: 1

Teja
Teja

Reputation: 13534

SELECT t2.ID,t3.MaskName,t3.Total
FROM Table2  t2 INNER JOIN  Table3 t3
ON t2.MaskID=t3.MaskID;

Upvotes: 2

Msonic
Msonic

Reputation: 1456

You may want to use a Join in your sql query. The w3schools site has a page explaining how to use it.

Upvotes: 0

Related Questions