MySQL Update not updating any rows - mysql

This is a weird issue I'm having, I have a table and try to do a MySQL-Update query, however, PHPMyAdmin keeps saying 0 rows affected.
Table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=55069 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `userid`, `name`, `last_login`) VALUES
(1, 55068, 'temp', '2012-02-02 09:04:50');
Query:
UPDATE `users` SET name='xorinzor' AND last_login=NOW() WHERE userid='55068'
No errors are returned, just nothing is happening, got no clue why that would be.
Regards,
Jorin

Change your update sentence to:
UPDATE `users` SET password='encryptedthingy', name='xorinzor', last_login=NOW()
WHERE userid=55068
Your SQL syntax was wrong. If you want to update multiple fields at once, you should not separate them with and keyword but with ,.
Also, make getting rid of single quotes for '55068' should help, since that column is a number. And '55068' is a string literal.
Make sure this sentence returns a value:
select * from `users` where userid=55068

Related

Update statement with Replace function results zero "0" value for all rows

I ran the following statement in our test mysql database, it works properly. However, when I ran it in production, I got all rows, including those of CLASS = 'ClassName', updated with XML value set to 0 !
UPDATE table_name c set c.XML= REPLACE (c.XML, '<flag_name>false</flag_name>', '')
WHERE c.CLASS = 'ClassName'
I want to know the reason behind this strange behaviour?
Note: The table structure is as follows:
CREATE TABLE `table_name` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CLASS` varchar(250) NOT NULL,
`XML` mediumtext NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=23510 DEFAULT CHARSET=utf8;
The expected behaviour is to remove the targeted tag

MySQL stop inserting when duplicate key condition is met

I have a table constructed by the followinng:
CREATE TABLE IF NOT EXISTS test_table (
ID int(11) NOT NULL AUTO_INCREMENT,
ProfileID int(11) NOT NULL,
ForeignID int(11) NOT NULL,
PRIMARY KEY (ProfileID,ForeignID) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I want to do something a little peculiar though, say there are 4 records in the database:
RecA, RecB, RecC, RecD
I would like to run the following query and have the insert behavior stop when a duplicate key was encountered:
INSERT IGNORE INTO test_table (ProfileID, ForeignID) VALUES(RecE, RecF, RecA, RecB, RecG);
So the query would only insert RecE and RecF, is there a way to do this in MySQL, perhaps using ON DUPLICATE KEY? Ideally the execution would just be terminated once a duplicate has been found, I am not too familiar with SQL syntax though.
Where RecG was explicitly not inserted.

MySQL CREATE TABLE IF NOT EXISTS in PHPmyadmin import

I have the following code
CREATE TABLE IF NOT EXISTS `abuses` (
`abuse_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL DEFAULT '0',
`abuser_username` varchar(100) NOT NULL DEFAULT '',
`comment` text NOT NULL,
`reg_date` int(11) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL,
PRIMARY KEY (`abuse_id`),
KEY `reg_date` (`reg_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Table with abuse reports' AUTO_INCREMENT=2 ;
this table already exists in the database, but when i import an sql file with phpmyadmin, the following error occurs
--
-- Dumping data for table `probid_abuses`
--
INSERT INTO `abuses` ( `abuse_id` , `user_id` , `abuser_username` , `comment` , `reg_date` , `auction_id` )
VALUES ( 1, 100020, 'artictundra', 'I placed a bid for it more than an hour ago. It is still active. I thought I was supposed to get an email after 15 minutes.', 1338052850, 108625 ) ;
#1062 - Duplicate entry '1' for key 'PRIMARY'
i thought because it already exists it won't attempt to create it, why is it behaving as such?
On the CREATE TABLE,
The AUTO_INCREMENT of abuse_id is set to 2. MySQL now thinks 1 already exists.
With the INSERT statement you are trying to insert abuse_id with record 1. Please set AUTO_INCREMENT on CREATE_TABLE to 1 and try again.
Otherwise set the abuse_id in the INSERT statement to 'NULL'.
How can i resolve this?
it is because you already defined the 'abuse_id' as auto increment, then there is no need to insert its value. it will be inserted automatically. the error comes because you are inserting 1 many times that is duplication of data. the primary key should be unique. should not be repeated.
the thing you have to do is to change your insertion query as below
INSERT INTO `abuses` ( `user_id` , `abuser_username` , `comment` , `reg_date` , `auction_id` )
VALUES ( 100020, 'artictundra', 'I placed a bid for it more than an hour ago. It is still active. I thought I was supposed to get an email after 15 minutes.', 1338052850, 108625 ) ;
Depending on what you want to accomplish, you might replace INSERT with INSERT IGNORE in your file. This will avoid generating an error for the rows that you are trying to insert and already exist.
See http://dev.mysql.com/doc/refman/5.5/en/insert.html.
If you really want to insert this record, remove the `abuse_id` field and the corresponding value from the INSERTstatement :
INSERT INTO `abuses` ( `user_id` , `abuser_username` , `comment` , `reg_date` , `auction_id` )
VALUES ( 100020, 'artictundra', 'I placed a bid for it more than an hour ago. It is still active. I thought I was supposed to get an email after 15 minutes.', 1338052850, 108625 ) ;
In your case, the first value to insert must be NULL, because it's AUTO_INCREMENT.

Mistakenly added duplicate records. How do I delete them?

EDIT
This query is not working, any idea why?
select `key`, distinct `filename`, `url`, `processed`, `timestamp` from snaps;
It says to check syntax near 'distinctfilename``
I have the following table
CREATE TABLE IF NOT EXISTS `snaps` (
`filename` varchar(255) COLLATE utf8_bin NOT NULL,
`url` text COLLATE utf8_bin NOT NULL,
`processed` int(11) NOT NULL,
`timestamp` int(11) NOT NULL,
KEY `key` (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I exported the rows of the table and mistakenly imported it back into the same table. So now I have two rows of everything. filename is unique.
How can I delete the duplicate records?
SELECT DISTINCT into a temporary table, flush the first one, and move them back again.
If you have all the original unduplicated rows of the table, can you not just
TRUNCATE <table> and then reimport them?
Does second set of rows has an identical timestamp from the insert? If so, you could DELETE based on that timestamp.
Alternately, you could SELECT INTO OUTFILE, delete the last half, TRUNCATE TABLE and then LOAD DATA INFILE.

how to prevent of duplicating records in mysql insertion queries?

i always in inserting data into a mysql table i use a select for that data before inserting to avoid duplicate records and if the query return null then i insert record.
but i think maybe it is not a professional way to do this job.
would you let me the ways you do?
if the reason you don't wish to use primary keys, or unique indexes is because of the error this will generate (which is an issue if you are inserting multiple rows on a single query), you can use the following syntax
insert ignore into [tablename] () VALUES ()
You can also use ON DUPLICATE KEY UPDATE as in to update certain fields as well.
insert into [tablename] () VALUES () ON DUPLICATE KEY UPDATE
for more information, have a look at http://dev.mysql.com/doc/refman/5.1/en/insert.html
You can try the following example. I would suggest you to try this first in your testing environment then you can implement this in your actual scenario.
Follow the below steps:
Step 1: create a table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Step 2: Run this query multiple times and check that only one row is inserted
INSERT INTO `student` ( `name`, `age`)
SELECT `name`, `age` FROM `student`
WHERE NOT EXISTS (SELECT 1
FROM `student`
WHERE `name` = 'manish'
AND `age` = '23'
);
The "professional" way to do this will be using a primary key constraint.
$qry="INSERT username INTO users";
if(!mysql_query($qry))
{
if(mysql_errno()=1062)
{
echo 'Unique costraint violation!';
}
else
{
//other error
}
}