Arun
Arun

Reputation: 1472

Select values from two tables using loop

I have Two tables named

1.Categortable

Category

Mouse

Computer

Electronics

and the second table Texttable

category                   Text

Mouse                     Logitech Mouse

Computer                  LG Computer

Electronics               LG Electronics

Here I need To select Text for each category in Categortable from Texttable

Can any one help how to loop this to get the output.

Upvotes: 4

Views: 369

Answers (2)

sgeddes
sgeddes

Reputation: 62831

No need to do any looping here, a simple JOIN should work for you:

SELECT * 
FROM CategoryTable CT
    LEFT JOIN TextTable TT ON CT.Category = TT.Category

I've used a LEFT JOIN in case you want to return rows from your Category table which don't have a corresponding match in the Text table.


In case you only want the matching records, just replace the LEFT JOIN with an INNER JOIN.

Upvotes: 4

Vijay
Vijay

Reputation: 8451

try out this

Select ct.category, tt.Text from Categorytable ct
inner join Texttable tt on ct.category = tt.category

Upvotes: 1

Related Questions