I am trying to update a table in a mysql database, and am getting a syntax error. It is a MyISAM table if that matters.
Here is the sql
UPDATE product SET price=(price*1.0909)
JOIN product_to_category ON product.product_id = product_to_category.product_id
WHERE category_id =6
OR category_id =1
OR category_id =2
My goal is to get a list of products from 3 specific categories (information from the *product_to_category* table) and increase the price by about 10%. The price is contained in the product table.
From what I see in the documentation I can use join in the update statement, and I have done similar queries in the past.
This is a production website, which currently has about 40,000 products. If needed I can do a php script that will loop through the products and do it one by one, but it seems like I should be able to do it directly from mysql.
Your statement is a little bit messed up. SET follows after JOIN which is part of the UPDATE clause.
UPDATE product
JOIN product_to_category
ON product.product_id = product_to_category.product_id
SET price = price * 1.0909
WHERE category_id IN (1,2,6)
Related
I want to replace tags rows in my database news table product where the product name is equal to the product name in another table product.
I want to copy the tags column from the products database to the product database tags must be in the correct row like the name column must match both tables.
I tried a query but it send me an error:
Table to copy = product
Table from copy products
Condition (name=name) or (slug=slug)
In both Table:
UPDATE product
set tags = (
select tags
FROM products
where product.name = products.name
);
It gives an error:
Subquery returns more than 1 row
It works with:
UPDATE product
set tags = (
select tags
FROM products
where product.id = products.id
);
What I have found to better understand and make sure you ultimately want in the final update, yet also make sure you are getting the records intended. Write the select first.
select
np.name,
np.tags NowTags,
op.tags OtherTags
from
NowProduct np
JOIN OtherProduct op
on np.Name = op.Name
Once you have this confirmed, just change the SELECT to an UPDATE. You can use the "alias" for the table being updated. I found its best to make sure your query is correct to see what records WILL be queried and what the before and what you WANT updated values should be. Once good, then apply update.
update np set
tags = op.tags
from
NowProduct np
JOIN OtherProduct op
on np.Name = op.Name
I'm a complete novice at SQL so please be gentle...
I have a database of products. I would like to use SQL to move a batch of products to a different category, based on if a particular word is present in the products title, and also, only if the products quantity is zero.
The database includes 2 relevant tables:
1st table is products which has these 2 relevant columns: products_quantity and master_categories_id
2nd table is products_description and it has relevant column products_name (the title)
So, for an example, a product with the following title (stored in the column products name):
Archimede Seguso Murano Incalmo Millefiori Glass Bowl
I would like the SQL code to find this product (any any others that contain the word Archimede in the title), check if the stock quantity is 0, and if so, change the value of the column master_categories_id to 277 for those products.
The code I have so far come up with is:
UPDATE products
SET master_categories_id = '277'
WHERE products_quantity = '0' and products_name = 'Archimede';
However, this does not work as the column products_name is not in the products table. I have spent many hours searching for the correct method of doing this, with no luck, and would be eternally grateful for any help!
My website is built using zencart, and the above SQL was run using Zen Carts Admin "SQL Query Executor", but I can also run the SQL using phpMyAdmin if that helps. I am using MySQL version 5.
Sorry if I have left out anything obvious that you need to know, like I said I am a complete novice, but I will try to provide any other info you need. Thanks in advance!
Assuming you have a products id column in both tables that you can use to JOIN, you will need to JOIN the tables to do the UPDATE:
UPDATE products p
INNER JOIN products_description pd
on p.product_id = pd.product_id
SET p.master_categories_id = '277'
WHERE p.products_quantity = '0'
and pd.products_name = 'Archimede';
If you want to update any rows that contain Archimede, then you will have to use the LIKE operator in SQL:
UPDATE products p
INNER JOIN products_description pd
on p.product_id = pd.product_id
SET p.master_categories_id = '277'
WHERE p.products_quantity = '0'
and pd.products_name LIKE '%Archimede%';
The LIKE operator will find any rows that contain Archimede in the title.
I have 3 tables; products, variants, and stock {simplified}
PRODUCTS
id
name
discontinued (ENUM: 0 or 1)
etc
VARIANTS
id
product_id
colour
size
STOCK
id
variant_id
branch_id
When the user selects to discontinue the PRODUCT I set the discontinued flag to 1. That's fine, but I want to delete the PRODUCT and VARIANTS records altogether if there is no STOCK record of the product. Obviously I can do this using a SELECT query first in PHP but I would like to do it in one mySQL query. This is what I have so far, but it is returning an error from mySQL:
$query = "DELETE FROM prod_lines,
JOIN variants ON variants.lineid = prod_lines.id
WHERE prod_lines.id = '$lineid' AND
(SELECT COUNT(id) FROM stock WHERE stock.variantid = variants.id) = 0";
Can anybody out there help me come up with the right solution? Maybe there is a better way that doesn't even involve a subquery?
Thanks in advance
I assume that the product_line table in your query is the PRODUCTS table you describe in the beginning of your post.
DELETE prod_lines.* -- you must specify which table you are deleting from, because there are several tables in the FROM clause
FROM prod_lines
JOIN variants ON variants.lineid = prod_lines.id
LEFT JOIN stock ON stock.variantid = variants.id
WHERE prod_lines.id = #lineid -- your "$lineid" variable here
AND stock.id IS NULL; -- selects only items with no match in the "stock" table
http://sqlfiddle.com/#!2/40a21
Try something like this,the downside is though you have to have two php variables here and also an nested query that you may not be ok with
DELETE FROM (select COUNT(id) countstock FROM stock s,variants v WHERE s.variantid = v.id and v.productid='$lineid') prod
where prod.countstock>1 and prod.productid='$lineid'
I want to get some data out of my database that is similar to a receipt you get at the supermarket (just an example which suits kinda good to my real situation).
For example you get the 2 (always only 2) products 1 and 3. These products are stored in a seperated product database.
Your shopping result is stored in one database containing all the details like time, location and so on AND in 2 columns (product_1, and product_2). Again this shopping situation is only a comparison to my real situation so I know that this would not be a good database structure for a shopping list.
So now I would like to get the Whole receipt but instead of printing the product IDs I would like to have the Name and for example the price on it.
If I had only one product I would use this query:
SELECT `list`.`time`, `list`.`location`, `prod`.`prod_name`, `prod`.`prod_price`
FROM `shopping_list` `list`, `products` `prod`
WHERE `list`.`product_1` = `prod`.`prod_id`
But since I have two products I cannot just go on with
AND `list`.`product_2` = `prod`.`prod_id`
But how do you achive what I would like to have?
Thank you very much,
Phil
You'll need to join to the product table twice
e.g.
SELECT
`list`.`time`,
`list`.`location`,
`prod1`.`prod_name` `prod_name1`,
`prod1`.`prod_price` `prod_price1` ,
`prod2`.`prod_name` `prod_name2`,
`prod2`.`prod_price` `prod_price2`
FROM `shopping_list` `list`
INNER JOIN `products` `prod1`
ON `list`.`product_1` = `prod1`.`prod_id`
INNER JOIN `products` `prod2`
ON `list`.`product_2` = `prod2`.`prod_id`
I'm not sure what your business rules are so you may need to convert the second INNER JOIN to a LEFT JOIN if they need to always select two products.
i don't know your exact situation but usually field names like product_1 and product_2 indicate bad database design. however if you really need that you need to join the products table twice.
select
*
from
list l
, product p1
, product p2
where
l.product_1 = p1.product_id
and l.product_2 = p2.product_id
(this is oracle syntax but i think it will work also in mysql).
hth.
The previous answers work well if you can parse out the two lines from 1 database result row. If you want two lines from the database you could do a UNION query:
SELECT `list`.`time`, `list`.`location`, `prod`.`prod_name`, `prod`.`prod_price`
FROM `shopping_list` `list`, `products` `prod`
WHERE `list`.`product_1` = `prod`.`prod_id`
UNION
SELECT `list`.`time`, `list`.`location`, `prod`.`prod_name`, `prod`.`prod_price`
FROM `shopping_list` `list`, `products` `prod`
WHERE `list`.`product_2` = `prod`.`prod_id`
I have a posts table and a likes table. Many users can like a post. The posts table has a denormalized likesScore column that represents the number of likes a post has. I would like to update the posts.likesScore table by doing a COUNT(*) on the likes table #postId.
I have an idea of how to do it, but I can't get the syntax right:
UPDATE posts
SET likesScore = ...
WHERE posts.id = #postId
MySQL UPDATE statements support JOINs - use:
UPDATE POSTS p
JOIN (SELECT t.postid,
COUNT(*) AS cnt
FROM LIKES t
GROUP BY t.postid) l ON l.postid = p.postid
SET likesscore = l.cnt
The standard means is to use a subquery, but that requires correlation in the subquery and filtration in the WHERE clause -- otherwise there's a risk of updating records that don't have references in supporting tables.
I prefer not to store the count in a table, using a view because there's a lot of risk of the count being out of sync with the data every time a LIKE in this case is made.
UPDATE posts
SET likesScore = (SELECT count(*) FROM likes WHERE likes.id=posts.id)
WHERE ...