[TABLE 1]
[TABLE 2]
shop_name | whole_sales_amount |
---|---|
A | 20000 |
B | 40000 |
shop_name | locataion | representative_number |
---|---|---|
A | NY | 1234 |
A | LA | 1234 |
B | IL | 5678 |
I want to join Table 1 & Table 2 (I know that the DB table structure is not good..)
And Plz do not use like (shop_name = A or representative_number = 1234) on where clause
[result]shop_name | whole_sales_amount | representative_number |
---|---|---|
A | 20000 | 1234 |
B | 40000 | 5678 |
2
Answers
So to achieve what you’ve been asking for you can join 2 tables via JOIN statement. In the example below I query all the required data from Table1 and used Join to include the required data from Table2.
It can be done like in this SELECT Query
You JOIN the table T1 and the table T2 by specifying the column they have in common in the JOIN. You can "see" the JOIN ON a little bit like it’s SELECT WHERE T1.shop_name = T2.shop_name
The use of GROUP BY is not necessary. I added it because based on your "result" you don’t seems to want to select every unique "shop_name". But if you are using the GROUP BY and there can be multiple possibility of "representative_number" for the same "shop_name", then don’t use the GROUP BY in my query
Here are the different type of JOIN, by default simply using JOIN is an INNER JOIN, so it will select only data that are in both table, if for example, in your table T2 you would have another shop_name "C", it wouldn’t select it because it isn’t in the table T1