I'm using MySql and I have Products table with columns ID, name, price (and others)
Usually you can update database like this:
Update Products
Set price = 100
where ID = 5
But what I want to update a value with column index instead of column name. Like this:
Update Products
Set "Third Column" = 100
where ID = 5
How to update table by column index?
Any ideas?
As per the comments you can do it with dynamic SQL (not recommended) due to security implications or the below.
Update Products
Set
ID = IF(colparameter=1, valueparam, ID),
name = IF(colparameter=2, valueparam, name),
price = IF(colparameter=3, valueparam, price)
where ID = 5
Even this I wouldn't recommend doing.
Related
I need to archive a product when it has been bought in the storefront. I`m thinking about doing a SQL JOIN but not sure...
Is this the query I need to run?
UPDATE product
INNER JOIN cart on cart.product_id = product.id
SET product.archive = 1
cart
id (int)
product_id (int)
quantity (int)
product
id (int)
name (varchar 45)
price (decimal 10,2)
quantity (int)
image (varchar 255)
archive (tinyint) default 0
That seems like a situation where you'd you want to use a trigger. Everytime a new line is added in the Cart table, it should update product.archive to 1 (or any other changes to the Product table).
DELIMITER $$
CREATE TRIGGER tg
AFTER INSERT ON Cart
FOR EACH ROW
BEGIN
UPDATE Product
SET archive = 1
WHERE Product.id = NEW.product_id;
END $$
Please take into account that I'm new to MySQL and I'm not sure if this is the right way to do this.
The update query
Let's examine this query closely
UPDATE product
INNER JOIN cart on cart.product_id = product.id
SET product.archive = 1
What does this query do? It archives every single product that has been placed into at least one user's cart. Surely that's not what you want? Your cart table does not have a user id in it. HOw do you determine which item in the cart belong to which user? Please consider adding such a column to your table.
To archive or not to archive
Shouldn't a product be archived only when it's quantity reaches zero? but should it be archived at all? If you have an archive column in your table in each query you will need to have a clause that checks this flag
SELECT * FROM product WHERE archive = 1 /* or 0 */
But this archive column can take only one of two values and that makes an index on that column completely useless. So as your data grows, your queries will become slower and slower.
I have a column ProductName in Products table. User is able to duplicate the product. While duplicating the product I have to copy all fields from the Products table for the particular product and create new record except for ProductName field. The ProductName field should be as follows:
for 1st duplicate: ProductName_Copy_1
for 2nd duplicate: ProductName_Copy_2
etc
Can anyone suggest me how to do this.
Use a subquery to generate the number:
insert into product
select
concat(productname, '_',
(select count(*) from product
where productname like concat(p.productname, '%')),
col2, col3, ....
from product p
where id = <original product id to copy>
Well if you are using phpmyadmin, there is an auto_increment attribute you can set to rows. Each row will be uniquely identified by that value.
More information can be found here
http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html
I have a column in a MySQL table which enters an auto-increment ID as new rows are added. Data from the MySQL table is pulled into an HTML table and the rows of the HTML table are sorted by the ID column. Normally this works fine for my purposes, but I now need to enter a row with a lower ID than the current auto-increment value, so that it appears further down the table.
So, I am wondering if there is a way, using a MySQL statement to add 1 to the ID values for a specific range of rows, e.g. add 1 to rows 800-850, so that they would now be 801-851, and then I could manually insert a new row with an ID value of 800.
You need to let the auto-incrementation working alone and create a new field table_id in your table to set the id number you want.
Reset table id is never a good idea : for example, don't forget all foreign keys linked on your table id.
To increment 1 to your specific id number, it would be :
UPDATE your_table SET table_id = table_id + 1 WHERE table_id BETWEEN 800 AND 850
update [yourtablename]
set [youridfieldoftable]=[youridfieldoftable] + 1
where [youridfieldoftable] >= 800
and [youridfieldoftable] <= 850
Be sure to substitute the table and field names with the correct ones. The field is the same four times.
In order to avoid the error
Duplicate entry x for key 'PRIMARY'
add the ORDER BY clause in the suggested query
UPDATE
your_table
SET
table_id = table_id + 1
WHERE
table_id BETWEEN 800 AND 850
ORDER BY
table_id DESC
I have a table which has a structure like as below.
create table test_table (id INT NOT NUll AUTO_INCREMENT
, name varchar(100),
primary key (id))ENGINE=INNODB
Select * from test_table;
id name
1 a
2 b
3 c
Now I want to increment the id by a number lets say 2
So the final results should be
Select * from test_table;
id name
3 a
4 b
5 c
The way I can do it is, first remove the PK and auto increment and then
update the table:
update test_table set id=id+2;
The other way is to make a temp table with out PK and auto increment and then
extract the result to the main table.
Is there any other way to do this without destroying the table structure ?
I am using MYSQL.
In your example, you need to remove the PK first to allow (temporary) duplicate id's during the course of the update.
To avoid duplicates, you must perform an ordered update:
UPDATE test_table SET id = id + 2 ORDER BY id DESC;
This will update records with largest value of id first, hence avoiding collision.
Obviously, if you want to decrement the values of id, then use "ORDER BY id ASC".
Here is the query to update the tables in SQL :- Its generic
UPDATE table_name SET column1=value, column2=value2,WHERE some_column=some_value;
Please follow the link for more information
Update Query
Thanks,
Pavan
Basically I have a table that versions products,
so it has two columns of interest, id | product_id
id is an autoincrement column,
product_id is just an int
When a product is first created the product_id comes from the id,
When the product is edited we duplicate the row, so the product_id is the same, but the id is different. so when we first create the product we do two queries,
insert, then update table whatever set product_id = id where id = {the insert id}
This works, but I am wondering if there is a way to do it in one query?
Note we only have access to insert, update, delete. no triggers or stored procedures.
Use the LAST_INSERT_ID() function:
update table whatever set
product_id = id
where id = last_insert_id()
This is the single query:
insert into whatever
set product_id = last_insert_id() + 1;