skip to Main Content

i would like to join two tables that have a common column for example "custcode". The table "customer" is filled with some customer data and the table "customer_address" with the customer addresses.Basically I would like to join the data of these tables so that I can export it.

here is a query that I have tried

SELECT
  *
FROM
  "customer"
  INNER JOIN 'customer_address' ON 'customer'.'custcode' = 'customer'.'custcode'
LIMIT
  1000

2

Answers


  1. The JOIN condition is not correct, you’re referencing the table "customer" twice. You have to reference the other table:

    SELECT * 
    FROM customer
      INNER JOIN customer_address 
        ON customer.custcode = customer_address.custcode 
    LIMIT 1000; -- you want an ORDER BY as well, to avoid random results
    

    Another option is to use USING():

    SELECT * 
    FROM customer
      INNER JOIN customer_address USING(custcode) 
    LIMIT 1000;
    

    You don’t need, nor want, double quotes " around your object names. Single quotes ‘ are used for content, not for object names.

    Login or Signup to reply.
  2. The query you provided has a couple of issues. First, you should use backticks (`) or square brackets ([ ]) to escape column and table names with spaces or special characters, not single quotes (‘ ‘). Second, it seems like you are using single quotes around the table names ‘customer’ and ‘customer_address,’ which would treat them as string literals, not table names

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