How to check that table has empty rows and then delete them - mysql

In my SQL tables there are rows where columnX has empty value (""). Now i want them i queried to select them and then delete them.
Query like:
tables has empty rows
Delete empty rows
How can i do this. Any idea

Depending on what exactly you mean by "empty" rows:
delete from yourTable where column1 is null
will delete where column1 has a null value. If you mean where multiple columns have nulls, it's just a matter of adding more conditions to the where clause:
delete from yourTable where column1 is null and column2 is null and column3 is null
If by empty you mean "has spaces in a text field or the field is empty" you can use some of the builtin functions to find them for example:
delete from yourTable where trim(column1)=''
which would find a row in the table where column1 only has white space in it and so on.
You might want to have a read of this article that I wrote on SQL, join and the like - it has got a fair bit in it about selecting the right rows from the table - and in your case, replace the select.... from where... with a delete from where...
Having said all that, I would really wonder why you are inserting data into your table that you don't want in it?

You can check each field for null or the empty string like this:
DELETE FROM table WHERE (column1 IS NULL OR column1 = '') AND (column2 IS NULL OR column2 = '')
Just add the rest of your columns to the WHERE clause.

Simple : delete from Test_table where c1 is null,....and cN is null

Ok Try this, i hope u'll find your solution
What you need is, first get empty rows
Select * From table_name Where column_name = "";
Then Delete the empty rows
Delete From table_name Where column_name = "";
or don't write the select query only write the delete query
I hope this solve your problem

Related

How do I update the data in a mysql table called link in the type column for user_id NULL?

I understand almost nothing in sql, help me update the data so that in the link table for the type column, change the value to direct, provided that the user_id column is NULL
Image
I only know how to update the entire type column, but I don't know how to do with the condition, only for user_id is NULL
Update links set type='direct'
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
See docs: https://dev.mysql.com/doc/refman/8.0/en/update.html
Update links set type='direct' WHERE user_id is null

Updating blank values with nulls in all the columns in a table

I have a table with 50 columns. This table contains nulls in all the columns but in random. I want to update all the nulls with 'no data'. If there are nulls in 2 columns I can do it with
update tableName
set col1 = 'nodata'
where col1 is null
But, I have 50 columns like this, instead of writing an update command for all the columns, is there any other technique to complete it in a single line query.
You can use coalesce():
update tableName
set col1 = coalesce(col1, 'nodata'),
col2 = coalesce(col2, 'nodata'),
. . .;
If most rows have data in all columns, you can add a where clause:
where col1 is not null or col2 is not null or . . .
Note: This answers your question, but I don't think it is a good idea. NULL is a perfectly good way to represent "no data" in a SQL database. There is no need to update the values. In addition, this assumes that all columns are strings. This method will not work on other data types -- you will likely get a type conversion error.

Update column with value from another column

In a SQL table I have two columns: the first contain a path and the second contains a value.
colunm1
/path/
colunm2
12345
I need to update the first column with the value that exists in the second column. to get this result :
colunm1
/path/12456/
I tried this, but not working
update tablename p
set p.colunm1 = "/path/'colunm2'/"
You have the right idea, but the SQL you shared uses column2 as a string literal. You could use the concat to concatenate the two columns:
UPDATE tablename
SET column1 = CONCAT(column1, column2)
You have to use CONCAT
update tablename p
set p.colunm1 = CONCAT("/path/",`colunm2`,"/");

mySQL Query Remove Null Records in Column

I have a large mySQL database and I want to remove every record that is empty, not null in a certain column. What is the best way to do write a SQL query for this?
Currently I have tried:
DELETE FROM Businesses WHERE WEBADDRESS IS NULL
But it did not delete anything. There are 44,000 records and almost 80% of them are null in that column.
DELETE FROM myTable WHERE myColumn IS NULL
Link to MySQL page for DELETE syntax: http://dev.mysql.com/doc/refman/5.7/en/delete.html
IF the column is not NULL but just blank you would need to do something like:
DELETE FROM myTable WHERE myColumn = ''
Based on the information you also provided in the comments, the values are likely being loaded as empty ('') and not NULL: http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html
The second query should work.
delete from your_table
where certain_column is null
DELETE from `<tablename>`
WHERE `<columnname>` is null
delete from table_name where column=''
delete from table_name where column_name is null;
this query will definitely work for you.
Don't worry about count of data. Mysql can handle it. Just write:
DELETE FROM `Businesses` WHERE `WEBADDRESS` = '';

Is it really no solution to update multiple records in MySQL?

I want to do all these update in one statement.
update table set ts=ts_1 where id=1
update table set ts=ts_2 where id=2
...
update table set ts=ts_n where id=n
Is it?
Use this:
UPDATE `table` SET `ts`=CONCAT('ts_', `id`);
Yes you can but that would require a table (if only virtual/temporary), where you's store the id + ts value pairs, and then run an UPDATE with the FROM syntax.
Assuming tmpList is a table with an id and a ts_value column, filled with the pairs of id value, ts value you wish to apply.
UPDATE table, tmpList
SET table.ts = tmpList.ts_value
WHERE table.id = tmpList.id
-- AND table.id IN (1, 2, 3, .. n)
-- above "AND" is only needed if somehow you wish to limit it, i.e
-- if tmpTbl has more idsthan you wish to update
A possibly table-less (but similar) approach would involve a CASE statement, as in:
UPDATE table
SET ts = CASE id
WHEN 1 THEN 'ts_1'
WHEN 2 THEN 'ts_2'
-- ..
WHEN n THEN 'ts_n'
END
WHERE id in (1, 2, ... n) -- here this is necessary I believe
Well, without knowing what data, I'm not sure whether the answer is yes or no.
It certainly is possible to update multiple rows at once:
update table table1 set field1='value' where field2='bar'
This will update every row in table2 whose field2 value is 'bar'.
update table1 set field1='value' where field2 in (1, 2, 3, 4)
This will update every row in the table whose field2 value is 1, 2, 3 or 4.
update table1 set field1='value' where field2 > 5
This will update every row in the table whose field2 value is greater than 5.
update table1 set field1=concat('value', id)
This will update every row in the table, setting the field1 value to 'value' plus the value of that row's id field.
You could do it with a case statement, but it wouldn't be pretty:
UPDATE table
SET ts = CASE id WHEN 1 THEN ts_1 WHEN 2 THEN ts_2 ... WHEN n THEN ts_n END
I think that you should expand the context of the problem. Why do you want/need all the updates to be done in one statement? What benefit does that give you? Perhaps there's another way to get that benefit.
Presumably you are interacting with sql via some code, so certainly you can simply make sure that the three updates all happen atomically by creating a function that performs all three of the updates.
e.g. pseudocode:
function update_all_three(val){
// all the updates in one function
}
The difference between a single function update and some kind of update that performs multiple updates at once is probably not a very useful distinction.
generate the statements:
select concat('update table set ts = ts_', id, ' where id = ', id, '; ')
from table
or generate the case conditions, then connect it to your update statement:
select concat('when ', id, ' then ts_', id) from table
You can use INSERT ... ON DUPLICATE KEY UPDATE. See this quesion: Multiple Updates in MySQL
ts_1, ts_2, ts_3, etc. are different fields on the same table? There's no way to do that with a single statement.