MySQL INSERT INTO using the WHERE clause - mysql

I'm trying to read all the SKU numbers from my product table and add those as tags into my product description table.
Here is the query I'm using:
INSERT INTO product_description (tag)
SELECT sku
FROM product p
WHERE p.product_id = pd.product_id; #I don't know where to define pd
Neither of these work:
INSERT INTO product_description pd (tag)
INSERT INTO product_description (tag) pd
I'm having difficulty figuring out how the WHERE clause should be constructed. I need to make sure the product IDs match from both tables and I can't give the first table an alias either.
To clarify my question, I'm actually trying to update my existing data in the product_description table, I'm not trying to add/insert new rows.

I believe you are looking for an update query here, as you are setting the value in one table based on it's relation to another.
To do this, you can JOIN the tables together using the related column, and set the values accordingly:
UPDATE product_description pd
JOIN product p ON p.product_id = pd.product_id
SET pd.tag = p.sku;
Here is an SQL Fiddle example.

INSERT INTO product_description (tag)
(SELECT sku
FROM product p
INNER JOIN product_description pd ON p.product_id = pd.product_id

I'm trying to read all the SKU numbers from my product table and add
those as tags into my product description table.
I read this as wanting something like this:
INSERT INTO product_description (tag)
SELECT distinct sku
FROM product p;
If you don't want duplicates, then be sure that you have a unique index/constraint on product_description(tag).
You don't give the structure of the table, but you might want the product id along with the tag:
INSERT INTO product_description (product_id, tag)
SELECT product_id, sku
FROM product p;

Related

How to select multiple fields in MySQL query when using INNER JOIN

I want to select multiple fields from table p.
The second line of this code is wrong. how to write?
except p.*
I don't want p.*
SELECT
p.id, title, price
c.`title` as `CategoryTitle`
from `tbl_products` p
INNER JOIN `tbl_categories` c
ON p.`category_FK` = c.`id`
Well, you might have your own reason so, perhaps you can do something like this:
SELECT
id, title, price, CategoryTitle
FROM `tbl_products` p
INNER JOIN
(SELECT `title` AS 'CategoryTitle', id AS 'CategoryID'
FROM `tbl_categories`) c
ON category_FK = CategoryID
Make one of the table as a subquery and define column alias that's not a duplicate with the other table. In your example, it seems like both of your tables have columns with similar names like id & title. Once you define those similar column names in the subquery with different alias, then you won't need to do p.xx or c.xx.

Get Min Price From Product Variation Table in MySQL

Product Table
Variation Table
How do I get a result from Product with every product's min price from Variation?
I join by description, which should work if no different products have the same description. It would be better to join by something more specific like product_id, but product_table does not have that as a column.
SELECT
p.*,
v.min(price) as min_price,
FROM
variation_table v
JOIN
product_table p ON v.description = p.description;
You could probably add ORDER BY product_id to do in order of the product ids or whatever other field you'd prefer.

How to Search on Joined Tables with multiple column?

I keep getting errors saying t.product_id column is unknown.
I used this code:
SELECT
t.product_id as ID,
product_category as Category,
product_name As Product,
product_price as Price,
product_decription as Description,
t.Stats as 'Status',
tblinventory.quantity
FROM tblproduct as t
INNER JOIN tblinventory
on t.product_id = tblinventory.product_id
WHERE
CONCAT(`t.product_id`,`product_category`,`product_name`,`t.Stats`) LIKE '%"2"%'
I just cant seem to make it work.
tblproduct has columns product_id,product_category,product_name,product_price,product_description and stats
while tblinventory has columns product_id and quantity
In MySQL, the `t.product_id` refers to a column with that name -- "t.product_id" rather than the column product_id in the table t. Remove the escape characters:
WHERE CONCAT(t.product_id, product_category, product_name, t.Stats) LIKE '%"2"%'
You don't need to escape the names, so don't.

Joining four mySQL tables without a single common value

I have four tables that I need to pull data from. Three of the tables have a common value (product_id) the fourth table has a value in common with the third table (category_id). So they look something like this:
Products:
product_id, name, quantity, image, weight
Product_Description:
product_id, Description
Product_to_Category:
product_id, category_id
Category:
category_id, category_name
I need to pull all of the data and combine it into a result that looks like this
Name, Quantity, Image, Weight, Description, Category name
I know how to do a JOIN that works for the first three tables but I don't know how to add the into the results.
Just add an additional join on the category_id:
select p.name, p.quantity, p.image, p.weight, pd.description, c.category_name
from products p
join product_description pd on p.product_id = pd.product_id
join Product_to_Category pc on p.product_id = pc.product_id
join category c on pc.category_id = c.category_id
A Visual Explanation of SQL Joins

How to create view in query having JOINS?

Hello I am not able to create view for following query.
create view my_View as
select * from product p
LEFT JOIN product_description pd ON (p.product_id=pd.product_id)
I have two tables
TABLE1 :-
Product with column as :- product_id, column2,column3
TABLE2 :- Product_Description with column as:- product_id , column4,column5.
Here product_id is foreign key for Product table.
Following is the error I am getting
Error Code: 1060. Duplicate column name 'product_id'
I am using mysql workbench to run my queries. Looking at the error, I understand that I need to assign alias. But I am required to join more than 5-8 tables thats the reason I am looking for better solution.
The Error is coming due to same column name in both the tables, second thing is its never a good practice to fetch all records in views by using * until and unless you really want all records from all table that you are using in join condition.
What you can do is select records specific to your view, like
select p.*,pd.somefield from product p
LEFT JOIN product_description pd ON (p.product_id=pd.product_id)
In the above query your product_id is fetched from product table only and you can fetch only necessary fields from product_description table
The easiest solution in your case is to use using instead of on:
create view my_View as
select *
from product p LEFT JOIN
product_description pd
USING (product_id);
In general, though, you should list the columns explicitly in the select rather than using *.
SELECT * will select all columns from both tables in the join. But both tables have a column called product_id, so you'll get two columns in the view with the same name.
You need to list out all the columns, so you can give an alias to product_description.product_id to distinguish it:
SELECT p.product_id, p.column2, p.column3, pd.product_id AS pd_id, pd.column4, pd.column5
FROM product AS p
LEFT JOIN product_description AS pd ON p.product_id = pd.product_id