Convert INSERT INTO into UPDATE [duplicate] - php

This question already has answers here:
Update a column in MySQL
(5 answers)
Closed 6 years ago.
Imagine I have some code like this:
$sql = "INSERT INTO tablename (column1, column2, column3) VALUES
('1','2','3')";
How would I convert this into an UPDATE query? I did some searching, but
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
doesn't make much sense to me. Thanks

INSERT and UPDATE queries look similar but they do different things.
Inserts => Inserts new nonexisting data into table
Update => Updates existing data inside of the table
That is why the UPDATE query has WHERE clause
So, you can use UPDATE to modify a row which is already inserted.

I don't know what exactly you are asking, because you are not clear in your question. But according to my thinking you want to know the difference.
Update query edit or update the data in the database. Like this
update table_name
set 'name'=something
where 'id'='some_id'
for more examples and practice go HERE
Insert insert query is just making a new data entry in the database in a table with a unique id, precisely.
HERE is the link for insert example.

Related

Is it possible to update values in MySql without using column names? [duplicate]

This question already has answers here:
Update MySQL without specifying column names
(5 answers)
Closed 8 years ago.
Hi is it possible to update MySql values without specifying column name..
for example
update table_name set name='john',rollnumber='30' where id='5';
i want update these john,30 values without specifying name,rollnumber..
is it possible?
You can use REPLACE INTO (which works like INSERT INTO) instead of UPDATE but I don't really recommend you to use it.
The REPLACE INTO method determines if it's an insert or an update, using the primary key value you specified. So you have the risk to insert things in your table.
Why it's a problem to specify column names ?
No, You can't.
Because, you do not update whole row.
You always update certain columns.
Refer it here:
http://dev.mysql.com/doc/refman/5.0/en/update.html
Try this
REPLACE INTO tbl_name (
PrimaryKey,
name,
rollnumber
) VALUES (
5,
'john',
'30'
Which is same as
UPDATE tbl_name
SET name = 'john', rollnumber = '30'
WHERE PrimaryKey = 5;

Getting all effected rown in MySQL UPDATE [duplicate]

This question already has answers here:
UPDATE/DELETE in mysql and get the list of affected row ids?
(3 answers)
Closed 9 years ago.
Title pretty much says it. I am using the deprecated mysql_ functions. I've got a query like:
UPDATE `table`
SET `row` = 'newtext'
WHERE `row` = 'oldtext'
Is there an easy way to get all effected row's primary key? Some glorious combination of mysql_insert_id and mysql_affected_rows? How can I do this without looping and doing each update one row at a time?
As a solution to your problem please refer the following sample code snippet
UPDATE `table` SET `row` = 'newtext' WHERE `row` = 'oldtext'
select id from table where row='oldtext'

MySQL: How to pre-append text to data in an existing column? [duplicate]

This question already has answers here:
How to prepend a string to a column value in MySQL?
(5 answers)
Closed 9 years ago.
I have a populated MySQL TABLE where I need to pre-append the text "NEW_" for the name field. (Long story, but this is what needs to be done for now.)
I imagine this can be done with an UPDATE but I'm not sure how to add "NEW_" to the start of the 'name' column. I also work in PHP, so I need to do this in PHP where I read each record, extract 'name' field, add this and then do an UPDATE for the record? Or can this all be done in MySQL?
use this query
UPDATE table_name set `name` = CONCAT('NEW_',`name`)
You can use CONCAT like this:
UPDATE tbl SET name=CONCAT('NEW_',name)
WHERE name NOT LIKE 'NEW\_%';
The second line will stop this operating on columns with NEW_ already at the start.
You can just do it like this:
UPDATE myTable SET name = CONCAT('NEW_',name);
As posted before by myself this will not work:
UPDATE myTable SET name = 'NEW_' + name;

Get inserted row ids in PHP and MySQL with multiple inserts [duplicate]

This question already has answers here:
Retrieving the last inserted ids for multiple rows
(2 answers)
Closed 9 years ago.
MySQL code is something like:
INSERT INTO table(name, value) VALUES ('name1', 'value1'), ('name2', 'value2'), ('name3', 'value3')
Basically, multiple inserts in the same SQL statement.
How can I get the IDs of the inserted values. I assume mysql_insert_id() combined with the number of inserts isn't safe since someone else might insert something at the same time.
Is there another way?
You either need to insert them one at a time or figure out the id's with a followup query.
INSERT INTO table(primkey, value) VALUES ('pk1', 'val1'), ('pk2', 'val2');
SELECT FROM table id, primkey where primkey in ('pk1', 'pk2');

MySQL - Insert where not exists [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to ‘insert if not exists’ in MySQL?
I have a few inserts and I only want them to actually insert if there isn't already the same information in there.
I only want the insert to run using WHERE NOT EXISTS, would anybody be able to help me adding that to my queries?
Queries:
$db->Query("INSERT INTO `surfed` (site) VALUES('".$id."')");
Thanks so much!
Use the IGNOREkeyword
INSERT IGNORE INTO surfed (user, site) VALUES('".$data['id']."', '".$id."')
If you have a unique key constraint on the user-site combination then it would normally generate an error on duplicates and abort the insertion. Using ignore in the insert statement will ignore the error and just not insert anything.
If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. ... With IGNORE, the row still is not inserted, but no error is issued.
See here for more infos about it
SQLFiddle example
You should be able to go with mysql_num_rows. If that returns a value of 0, then there is nothing in your table matching what you want to insert. (WHERE NOT EXISTS).
So make something like "if mysql_num_rows = 0 then insert else do nothing".
First your two columns need to be a combined key, then you can use INSERT IGNORE as outlined by juergen d or use
ON DUPLICATE KEY UPDATE user=user
See similar question here:
INSERT ... ON DUPLICATE KEY (do nothing)
For the revised question of only having a site column, make sure the site column is a unique key or primary key and then use
$db->Query("INSERT INTO `surfed` (site) VALUES('".$id."') ON DUPLICATE KEY UPDATE site=site");

Categories