I want to update a column in table INVENTAR from a couple of INNER JOINS.
I have the following:
Table INVENTAR with the column PRODUCT_ID,CATEGORY
Table PRODUCT_TO_CATEGORY with the columns PRODUCT_ID,CATEGORY_ID
Table CATEGORY_DESCRIPTION with the columns CATEGORY_ID, NAME
I want the NAME column to update the CATEGORY column.
Here's my code:
UPDATE inventar
SET inventar.category=category_description.name
FROM inventar
INNER JOIN product_to_category
ON product_to_category.product_id=inventar.product_id
INNER JOIN category_description
ON category_description.category_id=product_to_category.category_id
The correct MySQL syntax is:
UPDATE inventar i INNER JOIN
product_to_category ptc
ON ptc.product_id = i.product_id INNER JOIN
category_description cd
ON cd.category_id = ptc.category_id
SET i.category = cd.name;
Your syntax looks more appropriate for SQL Server or Postgres.
Related
I have a table in MySql Db named as newtbl which has 2 columns referenced as a foreign keys they are m_id, p_id. I want that instead of these m_id and p_id their names should be displayed.
Query which I wrote is below, but it has errors.
select newtbl.*, pa.p_name, m.m_id
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
You can try to column an alias name used as
select newtbl.*, pa.p_name, m.m_id as 'mmID'
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
Below is a answer for my question.
select newtbl.u_id, pa.p_name from newtbl left join players pa on newtbl.p_id = pa.p_id
I am trying to create a view in MySQL, however, an error occured:
Duplicate column name 'profil_id'
This is my request:
CREATE VIEW my_view
AS SELECT * FROM `profils` AS `p`
INNER JOIN `table_A` ON table_A.profil_id = p.id
INNER JOIN `table_B` ON table_B.profil_id = p.id
WHERE p.id = '1';
I know this is because I have to create an alias for profil_id, but I don't know how to figure out that with an INNER JOIN...
INNER JOIN `table_A` ON table_A.profil_id = p.id AS table_A_profil_id
Does not work.
Thank you for your help.
You can use the following:
CREATE VIEW my_view AS
SELECT `p`.*, `table_A`.`profil_id` AS table_A_profil_id, `table_B`.`profil_id` AS table_B_profil_id
FROM `profils` AS `p`
INNER JOIN `table_A` ON table_A.profil_id = p.id
INNER JOIN `table_B` ON table_B.profil_id = p.id
WHERE p.id = '1';
You can't use * in this case because you INNER JOIN two tables with the same column name. You need to list the columns for these two tables and set an alias to the duplicate column names.
I'm trying to delete all records form the table "oc_products" that don't have a certain category ID. I created a SELECT query that lists those products using an INNER JOIN, since the categories are in a separate table.
What I can't figure out is how to use the DELETE function to delete the shown records.
This is what my code looks like:
DELETE oc_product
FROM oc_product
INNER JOIN oc_product_to_category ON oc_product.product_id = oc_product_to_category.product_id
WHERE oc_product_to_category.category_id = 343
Its showing the error "Unexpected keyword, (near INNER JOIN)".
Add .* to p in your first line.
Try:
DELETE p.* FROM oc_product p
INNER JOIN oc_product_to_category pc ON p.product_id =
pc.product_id
WHERE pc.category_id = 343
In my other question I asked how to select a column from multiple tables with inner joins. My new question is: how to delete these results?
SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type"
If you want to delete only from products then the below should do the job
delete p from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
But if you need to delete from all the tables matching the joining condition then use
delete p,ipm,pi from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
You can use this query
DELETE FROM product_image WHERE product_image_id IN (SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type")
EDIT : From the manual
Currently, you cannot delete from a table and select from the same
table in a subquery.
If you want to modify the same query you can execute it by creating a temporary table (here its resultset)
DELETE FROM product_image WHERE product_image_id IN ( SELECT resultset.product_image_id FROM (SELECT
product_image.product_image_id
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type") AS resultset )
OR you can use the USING like this in the example from the MySQL Manual,
13.2.2 DELETE Syntax. I haven't used the USING, but you can definetely check out.
DELETE FROM t1, t2 USING t1
INNER JOIN t2
INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Also this SO post will help too MySQL Error 1093 - Can't specify target table for update in FROM clause
I have three tables, product, category and product_to_category. Product has the primary key product_id, category category_id and product_to_category p2c_id. Product_to_ category links products to categories in a many-to-many relationship using their respective ID's.
Basically I want to write a query that would select all products from categories that do not exist in the category table. This is due to products being migrated across from another database.
I had something like this but was a little lost.
SELECT *
FROM product AS p
LEFT JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
LEFT JOIN category AS c ON c.category_id
Basically that is as far as I have got. I need to join the category table to the product_to_category table where the product_to_category category_id is not in the category table. I may be completely on the wrong path but am stuck!
Thanks in advance.
Assumption: A product can be part of categories that exist, categories that do not exist, or no categories at all. You have not asked for products that belong to no categories at all, so the first LEFT JOIN from product to procduct_to_category should be an INNER JOIN.
Caveat: I am rusty at mysql so I am using SQL SERVER syntax. I forget if mysql has ON clauses or uses where clauses for JOINs. If ON clause is not supported, change them into WHERE clauses.
There are two common approaches: OUTER JOIN or a NOT IN clause (or a NOT EXISTS clause, which often behaves the same performance-wise as the NOT IN clause.)
OUTER JOIN
select p.*, p2c.category_id
from product p
INNER JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
LEFT JOIN category c ON p2c.category_id = c.category_id
WHERE c.category_id IS NULL
The test for null will find the unmatched records.
NOT IN clause
SELECT p.*, p2c.category_id
FROM product p
INNER JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
WHERE p2c.category_id NOT IN (SELECT c.category_id FROM category c)
If you're looking for products from nonexistent categories, I'd suggest
Select p.*,p2c.category_id
from product p
join product_to_category p2c
on p.product_id=p2c.product_id
left outer join category c
on p2c.category_id=c.category_id
where c.category_id is null
SELECT p.*
FROM product AS p
LEFT JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
WHERE NOT EXISTS (
SELECT 1
FROM category c
WHERE c.category_id = p2c.category_id
)
SELECT *
FROM product AS p
JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
JOIN category AS c ON c.category_id != as.category.
Try this?