skip to Main Content
[TABLE 1]
shop_name whole_sales_amount
A 20000
B 40000
[TABLE 2]
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


  1. 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.

        SELECT t1.shop_name, t1.whole_sales_amount, t2.representative_number
        FROM Table1 t1
        INNER JOIN Table2 t2 on t1.shop_name = t2.shop_name
    
    Login or Signup to reply.
  2. It can be done like in this SELECT Query

    SELECT T1.shop_name, T1.whole_sales_amount, T2.representative_number 
      FROM T1 JOIN T2 ON T1.shop_name = T2.shop_name 
    GROUP BY T1.shop_name;
    

    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
    Here are the different type of JOIN

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search