Update whole table records where column have specific value - mysql

I am working on a project in which a table has lot of records. most of the rows have 0 or empty record in column. Its not possible to update all manually or by writing query for each row or column.
Is there efficient way by which i can replace 0's by empty or empty by 0's and one more things..
Some columns have ",0" and "0," which also have to be replaced. here is screenshot of data in screenshot to give idea.. http://prntscr.com/8rvn67

Unfortunately, I can't access the screenshot. Howver, the general solution could be to list the values to be replaced in an IN() clause, if the list is small
update table set fieldname='0' where fieldname in ('',',0','0,")

To remove those 0,,0 to empty space '' you need to use next query
UPDATE table_name
SET column = REPLACE(column, '0', '')
WHERE column LIKE '%0%'
Hope it helps

Related

How to remove blank comments from table?

I have table and I have tried to remove comments but it gives me a blank comment still.
I have used
ALTER TABLE `b2b`.`mailsysy`
COMMENT = '' ;
to remove existing comment but it puts blak as comment so what to do if I want to remove all comment put only one comment on it.
EDIT
I have tried this also :
If you don't want to retrieve empty comments, then skip them in your query:
SELECT table_comment
FROM information_schema.tables
WHERE table_name ='xxx'
AND TABLE_COMMENT IS NOT NULL
AND TABLE_COMMENT <> ''
demo --> http://www.sqlfiddle.com/#!2/8664d/2
The column TABLE_COMMENT in TABLES and COLUMN_COMMENT in COLUMNS are part of those tables. They are not separated into any child tables. Due to the reason, though you set comments to be empty, the select query returns a row.
The other point you are ignoring is, you are selecting only a column from the TABLES table and omitting other columns though have a record satisfying your where condition. If the where condition is not satisfied, the query would have resulted Empty set (0.00 sec). In your case query resulted 3 rows on other data, that have comments set to empty. And hence you see 3 rows in set (0.01 sec).
I think you are expecting that if a comment is not defined, query should have not fetched a row from the table. If that was the requirements, please note that there is no such child table TABLE_COMMENTS or COLUMN_COMMENTS, which if defined correctly might have had a parent-child relation on TABLES and COLUMNS respectively. And on creation of a table unless you defined a comment it would have not inserted into the respective comments table. And when you query on TABLE_COMMENTS or COLUMN_COMMENTS table, it would have fetched Empty set... satisfying your where condition.

MySQL TRIM Issue

This is not working for me:
UPDATE emails SET address = TRIM(address);
0 rows affected. ( Query took 0.2440 sec )
Why?
Thanks.
Maybe they didn't actually need trimming. From the docs:
If you set a column to the value it currently has, MySQL notices this and does not update it.
UPDATE returns the number of rows that were actually changed.
In other words, they had no leading or trailing spaces.
If you really want to test this, use:
SELECT COUNT(*) FROM emails;
UPDATE emails SET address = CONCAT(' ', address);
UPDATE emails SET address = TRIM(address);
(that first one is to check the possibility of an empty table).
The simplest answer is that MySQL reports the correct number of rows, i.e., there was a total a zero rows changed by the update. That can happen if:
The table is empty.
There aren't any rows with leading or trailing whitespace (MySQL will not update a row if no column value change).

MySQL : multiple column values as 1 comma-separated string?

Suppose I have a select query like :
SELECT * FROM tablename
and in the table are columns : field1, field2 and field3
Does anyone know if it's possible to get a resultset with only 1 row with 1 field, with comma separated values of the columns, like this :
"fieldvalue1, fieldvalue2, fieldvalue3"
Problem is that I don't know the column names of the table in advance...
Another problem is that a prepared statement is not the way to go, since all this should be done from within a trigger, and MySQL doesn't allow dynamic cursors/selects inside a trigger.
I have done some research and only came as far as GROUP_CONCATenating the column names correctly. But the problem is, that
SELECT (SELECT GROUP_CONCAT( cols.column_name) FROM (SELECT column_name FROM information_schema.columns WHERE table_name='test_table') as cols) FROM test_table
will return one and the same concatenated string containing the column names, once for each table row, instead of evaluating it as the column names for the outer select statement and returning the actual values.
From what I have read in all of the forums discussing this kind of question (and there were many), there is really no way of getting this to work without prepared statements.
I can only think of one other way to do this, and that would be having a dedicated column on each table, where you concatenate the individual column values on INSERT or UPDATE, so you can simply select this one field instead of the full set of fields.
Seems like you have 3 questions here:
Getting a resultset with 1 row, 1 field: MYSQL has a CONCAT_WS function that works like this:
SELECT CONCAT_WS(',',Field1,Field2,Field3)
That will return "Field1Value, Field2Value, Field3Value"
I'm not sure how you are going to get these column names. Do you need to get them from a sql statement, a string, etc. ? You can get the table names `SHOW COLUMNS FROM tablename'. The Field column will have the column names.
Triggers are available in mysql (added in 5.0.2 I think): http://dev.mysql.com/doc/refman/5.0/en/triggers.html
First, to find out the columns' names in advance, assuming that you have the table's name, you can get them as any other query:
SHOW COLUMNS FROM your_table
Once you have the names you can do:
SELECT CONCAT(field1,',',field2,',',field3) AS newField FROM your_table

Strange data swapping error occurs when I attempt to update rows in my table from another table in MySQL database

So I have a table of data that is 10,000 lines long. Several of the columns in the table simply describe information about one of the columns, meaning, that only one column has the content, and the rest of the columns describe the location of the content (its for a book). Right now, only 6,000 of the 10,000 rows' content column is filled with its content. Rows 6-10,000's content column simply says null.
I have another table in the db that has the content for rows 6,000-10,000, with the correct corresponding primary key which would (seemingly) make it easy to update the 10,000 row table.
I have been trying an update query such as the following:
UPDATE table(10,000)
SET content_column = (SELECT content FROM table(6,000-10,000) WHERE table(10,000).id = table(6-10,000.id)
Which kind of works, the only problem is that it pulls in the data from the second table just fine, but it replaces the existing content column with null. So rows 1-6,000's content column become null, and rows 6-10,000's content column have the correct values...Pretty strange I thought anyway.
Does anybody have any thoughts about where I am going wrong? If you could show me a better sql query, I would appreciate it! Thanks
The reason is that you have no Where clause so all rows are being updated. The following query will only update tows that exist in the 6K-10K table, but will still overwrite matching values.
Update table(10,000)
Set content_column = (
Select content
From table(6,000-10,000)
Where table(10,000).id = table(6-10,000.id)
)
Where Exists (
Select 1
From table(6,000-10,000)
Where table(10,000).id = table(6-10,000.id)
)
Another way, if you simply do not want to overwrite the existing values would be to use Coalesce:
Update table(10,000)
Set content_column = Coalesce(table(10,000).content_column
, (
Select content
From table(6,000-10,000)
Where table(10,000).id = table(6-10,000.id)
))

mySQL UPDATE query returns "0 rows affected"

I have this query:
UPDATE phonecalls
SET Called = "Yes"
WHERE PhoneNumber = "999 29-4655"
My table is phonecalls, I have a column named PhoneNumber. All I want to update is a column named Called to "yes".
Any idea what I am doing wrong? when I return my query it says 0 rows affected.
If the such value already exists, mysql won't change it and will therefore return "0 rows affected". So be sure to also check the current value of called
Another reason for 0 affected rows that I have observed: wrong data type. If the column you want to update is an integer or boolean, and you set it to a string, it won't be updated - but you will also get no error.
To sum up the other strategies/ideas from this post:
Check with a SELECT statement, whether your WHERE works and returns results.
Check whether your columns do already have the value you want to set.
Check if your desired value suits the data type of the column.
If the values are the same, MySQL will not update the row (without triggering any warning or error), so the affected row count will be 0.
The problem might be that there are no records with PhoneNumber == "999 29-4655".
Try this query:
SELECT * FROM phonecalls where PhoneNumber = '999 29-4655'
If it doesn't return anything, then there are no rows that match.
For the benefit of anyone here from Google, this problem was caused by me because I was trying to append to an empty field using CONCAT().
UPDATE example SET data=CONCAT(data, 'more');
If data is NULL, then CONCAT() returns NULL (ignoring the second parameter), so the value does not change (updating a NULL value to be a NULL value), hence the 0 rows updated.
In this case changing to the CONCAT_WS() function instead fixed the problem.
Try select count(*) from phonecalls where PhoneNumber = "999 29-4655"; That will give you the number of matching rows. If the result is 0, then there isn't a row in the database that matches.-
Check to make sure this returns some result.
SELECT * FROM phonecalls WHERE PhoneNumber = '999 29-4655'
If it doesn't return any result than the filter WHERE PhoneNumber = '999 29-4655' is not correct.
Does it say Rows matched: 1 Changed: 0 Warnings: 0? Then maybe it's already set to that value.
Did you try single quotes vs. double quotes?
"999 29-4655" is the space a space or a tab and is it consistent in your query and the database?
That's my sugestion:
UPDATE `phonecalls` SET `Called` = 'yeah!' WHERE `PhoneNumber` = '999 29-4655' AND `Called` != 'yeah!'
And make sure with the case-sensitive name of table and field`s.
Just ran into an obscure case of this. Our code reads a list of records from the database, changes a column, and writes them back one by one. The UPDATE's WHERE clause contains only two conditions: WHERE key=? AND last_update_dt=?. (The timestamp check is for optimistic locking: if the record is changed by another process before we write ours, 0 rows are updated and we throw an error.)
But for one particular row the UPDATE was failing- zero rows effected.
After much hair-pulling I noticed that the timestamp for the row was 2019-03-10 02:59. In much of the U.S. that timestamp wouldn't exist- Daylight Savings Time causes the time to skip directly from 2:00 to 3:00. So I guessed that during the round trip from MySQL to Java back to MySQL, some part of the code was interpreting that timestamp differently from the rest, making the timestamps in the WHERE clause not match.
Changing the row's timestamp by one hour avoided the problem.
(Of course, the correct fix is to abolish Daylight Savings Time. I created a Jira but the U.S. Government has not responded to it yet.)
In my case, I was trying to update a column of text to correct a truncation problem with it. Trying to update to the correct text was yielding 0 rows updated because the text in the row wasn't changing.
Once I extended the column in the table structure to accommodate for the correct number of characters, I was able to see the desired results.