Here i already have a row which contains the values, i have a id as a primary key
now what i want to do is using that id i want to duplicate the same row values and create another row.except i want to update 2 column values. how can i do this?
for ex:
in one row i already have the values say
product_id,product_price,product_nos,product_remaining,product_name
now i want to duplicate the above row with changing only one column value
,say product_remaining set value to 10.
How can i do this in php or with query?
You can use the following query if you not use auto_incrementing ids, 10 is the new value for product_remaining.
INSERT INTO table (product_price,product_nos,product_remaining,product_name)
SELECT product_price,product_nos,10,product_name FROM table
WHERE product_id = 1
or this one where you set your id, where 999 is the new id:
INSERT INTO table (product_id,product_price,product_nos,product_remaining,product_name)
SELECT 999,product_price,product_nos,10,product_name FROM table
WHERE product_id = 1
Related
I have a table with 4 columns and multiple rows.
The first column „ID“ is set to be „0“ as default for each row.
Now, I want to UPDATE this table and SET „ID“ to be unique number for multiple rows, which match the specific query created by one submit in my form.
The point is, that ID number has to have the same value as other rows with submited parameters. The ID number should be automatically generated, but it can’t be 0 and it has to be unique for each query.
Example:
So, on submit it should generate ID for each row with these parameters. These parameters are already in my table, I just want to assign same ID.
Sorry for my English :)
insert into table(c2,c3,c4) (select IFNULL(1,Max(c1)+1),5,5 from table);
If your table is empty or first column is null then this query will insert 1 ,If it's not then it will get maximum value from the first column and add 1 . then it'll insert row into your table .
UPDATE AggregatedData SET datenum="734152.979166667",
Timestamp="2010-01-14 23:30:00.000" WHERE datenum="734152.979166667";
It works if the datenum exists, but I want to insert this data as a new row if the datenum does not exist.
UPDATE
the datenum is unique but that's not the primary key
Jai is correct that you should use INSERT ... ON DUPLICATE KEY UPDATE.
Note that you do not need to include datenum in the update clause since it's the unique key, so it should not change. You do need to include all of the other columns from your table. You can use the VALUES() function to make sure the proper values are used when updating the other columns.
Here is your update re-written using the proper INSERT ... ON DUPLICATE KEY UPDATE syntax for MySQL:
INSERT INTO AggregatedData (datenum,Timestamp)
VALUES ("734152.979166667","2010-01-14 23:30:00.000")
ON DUPLICATE KEY UPDATE
Timestamp=VALUES(Timestamp)
Try using this:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index orPRIMARY KEY, MySQL performs an [UPDATE`](http://dev.mysql.com/doc/refman/5.7/en/update.html) of the old row...
The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values...
This is not too bad, but we could actually combine everything into one query. I found different solutions on the internet. The simplest, but MySQL only solution is this:
INSERT INTO wp_postmeta (post_id, meta_key)
SELECT
?id,
‘page_title’
FROM
DUAL
WHERE
NOT EXISTS (
SELECT
meta_id
FROM
wp_postmeta
WHERE
post_id = ?id
AND meta_key = ‘page_title’
);
UPDATE
wp_postmeta
SET
meta_value = ?page_title
WHERE
post_id = ?id
AND meta_key = ‘page_title’;
Link to documentation.
I had a situation where I needed to update or insert on a table according to two fields (both foreign keys) on which I couldn't set a UNIQUE constraint (so INSERT ... ON DUPLICATE KEY UPDATE won't work). Here's what I ended up using:
replace into last_recogs (id, hasher_id, hash_id, last_recog)
select l.* from
(select id, hasher_id, hash_id, [new_value] from last_recogs
where hasher_id in (select id from hashers where name=[hasher_name])
and hash_id in (select id from hashes where name=[hash_name])
union
select 0, m.id, h.id, [new_value]
from hashers m cross join hashes h
where m.name=[hasher_name]
and h.name=[hash_name]) l
limit 1;
This example is cribbed from one of my databases, with the input parameters (two names and a number) replaced with [hasher_name], [hash_name], and [new_value]. The nested SELECT...LIMIT 1 pulls the first of either the existing record or a new record (last_recogs.id is an autoincrement primary key) and uses that as the value input into the REPLACE INTO.
I have a MySQL database with a table called chapters.
I have a column called chapter_number which is an Integer data type and is NOT auto increment as I have another field which uniquely identifies the row.
Upon insertion to this table I would like the value of the chapter_number column to be the previous row's chapter_number incremented by 1 (+1).
The purpose of this is to ensure that every time a chapter is added, the right ordering takes place.
How could I achieve this in PHP?
Here is an example of what I mean?
Table structure:
$stmt = $db->prepare('INSERT INTO chapters VALUES (?,?,?,?)');
$stmt->execute(array('','title', '2015-04-03', 'chapter 1 body'));
What would I put for the first parameter ^
SELECT max(chapter_number) + 1 as total FROM table;
This would also allow you to later on have chapters assigned to multiple books by symply adding a where clause
The following example is how this syntax would look
insert into table_one (greeting_column, name_column)
SELECT
'hello',
(SELECT max(chapter_number) + 1 as total FROM table)
I am trying to copy duplicate and insert data into a table like so...
ID, Value
1,1
1,2
1,3
Now what I want to do is an insert statement that will take all rows for ID '1' and insert it in again but with an ID of 2. The ID is not unique.
Is there a way to do this without looping through all rows and inserting one by one with a regular Select, then insert statement?
Try this:
insert into TABLENAME (id,value) select 2, Value from TABLENAME where id = 1
For example:
Row Name
1 John
2 May
3 Marry
4 Tom
5 Peter
Suppose I delete row 2 and row 3, is it possible to update Tom and Peter to row id 2 and 3 respectively and the next insert row to be row id 4?
yes, but you need to recreate Row:
ALTER TABLE `users` DROP `Row`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD `Row` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
No, because think of the problems that this could create. Imagine if you are managing a store inventory and each item has an internal barcode, based on an ID in a database table. You decide never to stock one item again and take it out of the database, and then every ID for every item with an ID greater than the removed item gets decremented by 1... all those barcodes on all those items become useless.
ID numbers in databases aren't generally meant to be used or referenced by people. They are meant to be a static index of a certain item in a database which allows the computer to pull up a given record quickly and reliably. When creating your ID field of your table, make sure you make the datatype large enough to account for these lost IDs when they do occur.
This is just a suggestion. I don't say this is the best solution. Just consider.
You execute your delete query.
DELETE FROM table_name WHERE Row IN (2,3);
Once deleted you make a select query request with PHP and get the data set to an array.
SELECT Row, Name from table_name ORDER BY Row ASC;
Then make a UPDATE execution using a loop.
$index = 1;
foreach($dataset as $item)
{
// Your update query
$sql = "UPDATE table_name SET Row=$index where Name='" . $item['Name'] . "'";
$index++;
}
Before you insert next query you have to get the max value of Row and set +1 value as the Row of the insert query.
This is just an idea. Not the complete code.