I'm trying to use this query
UPDATE products
SET products.product_price = '87.00000'
FROM products
INNER JOIN product_category
ON products.product_id = product_category.product_id
WHERE product_category.category_id = '64'
However I receive this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM table products INNER JOIN table product_category ON prod' at line 3
I don't see any syntax error. I have made this query from examples on this forum.
remove the FROM products line.
and put the
SET ... line just before the WHERE clause.
to be clear :
UPDATE ...
JOIN ...
SET ...
WHERE ...
you could also do
UPDATE products p
SET p.product_price='87.00000'
WHERE EXISTS (SELECT NULL
FROM product_category pc
WHERE p.product_id = pc.product_id
AND pc.category_id = '64');
You can use the following:
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64';
See SQL Fiddle with Demo
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
I think the SET clause needs to be after the table references and before the WHERE clause, like this:
UPDATE products
INNER
JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
Here's how I get that syntax when I need an UPDATE like that. I always start with a select statement, and that lets me know which rows are going to be updated. For example:
SELECT p.*, c.*
FROM products p
JOIN product_category c
ON p.product_id = c.product_id
WHERE c.category_id = '64'
To convert that to an update statement, I add a "SET" clause after the table references and before the WHERE clause, and then replace "SELECT ... FROM" with the "UPDATE" keyword. Voila.
Related
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
I've got the following SQL query and I'm trying to implement pagination, so I first want to get the COUNT of the result:
The normal query (works fine)
SELECT DISTINCT c.*, p1.*, username FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL ORDER BY c.firstname ASC
I've tried the following, but it throws an error and I'm not sure what correct syntax to use:
Attempting to count the results (doesn't work)
SELECT COUNT(DISTINCT c.*, p1.*, username) FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL ORDER BY c.firstname ASC
The error:
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', p1., username) FROM candidate c LEFT ' at line 1
One option is to use a subquery:
SELECT COUNT(*)
FROM (
SELECT DISTINCT c.*, p1.*, username FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL
) t
Depending on your data, you may be able to do this without the subquery, but you cannot use multiple columns with the count aggregate -- that's what is causing your error.
Morning everyone,
I have wrote a SQL query to fetch all the data from database I want updated but can work out how i would put an update statement in here.
SELECT product_id, product_title, prodcat_category_id, category_active, product_active from products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id
LEFT JOIN categories on productcategories.prodcat_category_id = categories.category_id
WHERE category_active = 0
I want to update all these results to have product_active 0, but I've never done an update statement inside of a select join.
Try below query
Update products set product_active = 0 where product_id IN
(
SELECT product_id from products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id
LEFT JOIN categories on productcategories.prodcat_category_id = categories.category_id
WHERE category_active = 0
);
Instead of selecting using joins, update with the joins.
update products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id
LEFT JOIN categories on productcategories.prodcat_category_id = categories.category_id
set product_active = 0
WHERE category_active = 0
I am getting the following error:
1052 - Column 'product_id' in field list is ambiguous
When I run the following:
SELECT `product_id`, `product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
AND `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`
My PRODUCTS table has
product_id, product_name, etc
My products_has_product_category table has
product_id, category_id
This is my first try at a join, so I appreciate the help!
You need to specify which table the product_id comes from. Since the product_id is in both tables, when you SELECT it you need to specify which table you want the value from. With a table alias:
SELECT p.product_id, p.product_name
FROM `products` p
INNER JOIN `products_has_product_category` pc
ON p.product_id = pc.product_id
AND pc.category_id = 1
ORDER BY p.product_name
Without table aliases:
SELECT `products`.`product_id`, `products`.`product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
AND `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`
If I understand your intention correctly, you probably meant WHERE rather than AND:
SELECT `products`.`product_id`, `products`.`product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
WHERE `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`