Saud
Saud

Reputation: 17

SQL joining table

I have this piece of code:

SELECT *
FROM products
JOIN productsInventory
WHERE sku = '$psku' AND sell_price = '$sell_price'

Where, there are two tables named products & productsInventory and sku & sell_price are their column names respectively. Is it the right way to write sql?

Upvotes: 1

Views: 58

Answers (3)

yohannist
yohannist

Reputation: 4204

You forgot to specify with which columns you are joining the tables using the ON clause. In order to join tables, you usually need to have some column both tables have in common.

SELECT * FROM products 
JOIN productsInventory on products.<CommonColumn> = productsInventory.<CommonColumn>
WHERE sku = '$psku' AND sell_price = '$sell_price'

Upvotes: 0

Ravi Matani
Ravi Matani

Reputation: 822

First of all you need to understand how join works in sql. For that you can refer http://www.dofactory.com/sql/join.
You should specify on which column you want to join two tables in on clause.
In your code common column in products table and productsInventory table should be specified in on clause of join.

Upvotes: 1

molig
molig

Reputation: 324

Usually it looks like this:

SELECT * FROM products AS l 
JOIN productsInventory AS r 
ON l.sku = r.sku
AND l.sell_price = r.sell_price;

Dpending on the sql dialect you are using that may vary.

Upvotes: 0

Related Questions