Select values ​​from two tables using a loop

I have two tables named

1.Categortable Category Mouse Computer Electronics 

and the second texttable

 category Text Mouse Logitech Mouse Computer LG Computer Electronics LG Electronics 

Here I need to select text for each category in Categorable from Texttable

Can anyone help how to do this to get the result.

+4
source share
2 answers

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

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

I used LEFT JOIN if you want to return rows from the Category table that do not have a matching match in the Text table.


If you only need matching records, just replace LEFT JOIN with INNER JOIN .

+4
source

try this one

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

Source: https://habr.com/ru/post/1483140/


All Articles