I'm using MySQL with InnoDB and I want to join the location column from products table into a productStock table. I have this table 'products'
mysql> select * from products;
+-----------+-------------------+-------+-------+-------------------------+-------+------------------+-------------+
| productID | product_reference | brand | model | productName|price|product_description | location |+-----------+-------------------+-------+-------+-------------------------+-------+---------------------+-------------+
| 1 | 0001 | HP | 6800 | Cartridge Plotter Z6100 | 40.00 | HP INK CARTRIDGE | Container 1 |
| 2 | 0002 | HP | 7550 | HP INK CARTRIDGE YELLOW | 10.00 | HP INK CARTRIDGE | Container 2 |
| 3 | 0003 | HP | 8600 | HP INK CARTRIDGE CYAN | 5.00 | HP INK CARTRIDGE | Container 1 |
| 4 | 0004 | HP | 8640 | HP INK CARTRIDGE RED | 5.99 | HP INK CARTRIDGE | Container 2 |
+-----------+-------------------+-------+-------+-------------------------+-------+---------------------+-------------+
and second table productStock
mysql> select * from productStock;
+----+-----------+-------------------+-------------------------+-------+-------+---------------+---------------+---------------+---------------------+
| id | productID | product_reference | productName | brand | model | physicalStock | lowStockAlert | lastUpdatedBy | lastUpdateDate |
+----+-----------+-------------------+-------------------------+-------+-------+---------------+---------------+---------------+---------------------+
| 1 | 1 | 0001 | Cartridge Plotter Z6100 | HP | 6800 | 33 | 0 | steven | 2014-09-25 13:36:35 |
| 2 | 2 | 0002 | HP INK CARTRIDGE YELLOW | HP | 7550 | 20 | 0 | osama | 2014-09-25 14:08:37 |
+----+-----------+-------------------+-------------------------+-------+-------+---------------+---------------+---------------+---------------------+
My joining code is sadly not working:
select ps.product_reference,
ps.productName,
ps.physicalStock,
ps.location as 'location'
from productStock as ps
join products ON ps.location = products.location;
This is the error I get:
ERROR 1054 (42S22): Unknown column 'ps.location' in 'field list'
All I want is to show the location for each product by getting location from products table.
this is my show create table for products & productStock:
mysql> show create table products;
------------------------------+
products
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| products | CREATE TABLE(
productID
int(10) NOT NULL AUTO_INCREMENT,
product_reference
varchar(255) NOT NULL,
brand
varchar(45) NOT NULL,
model
varchar(45) NOT NULL,
productName
varchar(255) NOT NULL,
price
decimal(7,2) unsigned DEFAULT NULL,
product_description
varchar(255) DEFAULT NULL,
location
varchar(45) NOT NULL,
productID
PRIMARY KEY (,
product_reference,
model,
productName),
product_reference
UNIQUE KEY(
product_reference),
model
UNIQUE KEY(
model),
productName
UNIQUE KEY(
productName)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 |
+----------+---------------------------------------------
for the productStock table:
mysql> show create table productStock;
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------+
productStock
| Table | Create Table |
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| productStock | CREATE TABLE(
id
int(10) NOT NULL AUTO_INCREMENT,
productID
int(10) NOT NULL,
product_reference
varchar(255) NOT NULL,
productName
varchar(255) NOT NULL,
brand
varchar(45) NOT NULL,
model
varchar(45) NOT NULL,
physicalStock
int(10) unsigned DEFAULT '0',
lowStockAlert
int(10) DEFAULT '0',
lastUpdatedBy
varchar(45) NOT NULL,
lastUpdateDate
timestamp NULL DEFAULT NULL,
id
PRIMARY KEY (),
productID
UNIQUE KEY(
productID),
product_reference
UNIQUE KEY(
product_reference),
productName
UNIQUE KEY(
productName),
model
UNIQUE KEY(
model),
productStock_ibfk_1
CONSTRAINTFOREIGN KEY (
productID) REFERENCES
products(
productID) ON DELETE CASCADE ON UPDATE CASCADE,
productStock_ibfk_2
CONSTRAINTFOREIGN KEY (
product_reference) REFERENCES
products(
product_reference) ON DELETE CASCADE ON UPDATE CASCADE,
productStock_ibfk_3
CONSTRAINTFOREIGN KEY (
productName) REFERENCES
products(
productName) ON DELETE CASCADE ON UPDATE CASCADE,
productStock_ibfk_4
CONSTRAINTFOREIGN KEY (
model) REFERENCES
products(
model) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 |
+--------------+------------------------------------------
Best Answer
Well unfortunately your query is wrong.
There is no
location
column in theps
table as you have declared, so you cannot use it as the key for joining the two tables together.Try this and use
product_reference
as your joining key, as it exists in both tables:As another hint: There is redundancy in your
productStock
table, e.g.productName
,brand
,model
can be retrieved from theproduct
table and should be dropped from theproductStock
table.