insert and remove multiple values in a table created with SET sql - mysql

I created a SET sql table, so a row can contain multiple values: Foo_1 Foo_2 Foo_3.
The above query run but it will update the the row and set just one value.
QueryType valid values are 1 2 or 3
"UPDATE Foo_status
SET type = 'Foo_%s',
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
When i run the query: Let's assume that QueryType is 1, it's working.. my type row will have Foo_1, but when i run again the query and Foo has QueryType 2, it will remove Foo_1 and add Foo_2
How i can change my query so when i run the query it will continue to update values ?
Example:
Run Query
-Add Foo_1
Run Query
-Add Foo_2
Table struct
CREATE TABLE `Foo_status` (
`name` text NOT NULL,
`type` set('Foo_1','Foo_2','Foo_3') CHARACTER SET latin2 NOT NULL DEFAULT '',
`status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I tried to search for an solution but without succes, that's i asked here.
Thanks!

Multiple set elements are specified using a comma-separated string. To add a new value, you have to concatenate a string beginning with comma.
"UPDATE Foo_status
SET type = CONCAT(type, ',Foo_%s'),
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
To remove a value from a SET, use REPLACE():
"UPDATE Foo_status
SET type = REPLACE(type, 'Foo_%s', ''),
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
It would be easier if you normalized your schema and used a table with a separate row for each type, instead of using the SET datatype. This would also allow you to add an index that would make searching for specific set values more efficient.

You can remove a set wirh this. Here written for select.
You can it also use for UPDATE.
You only must change the string to remove
select t,
trim( BOTH ',' FROM
replace (concat (',',t,','),',Foo_2,',',')) as new_t
from F;
Sample: http://www.sqlfiddle.com/#!9/a8b041/2

Related

Set value=' '(empty String) where the field value is NULL (multiple columns)

I have a table contains lots of column. Many columns contain NULL value. But while I'm trying to create CSV file from it, NULL is being replaced by '/N'. I want to set all the columns value to empty string instead of NULL so that I don't have to face problem with '/N'
You can use FIELDS ESCAPED BY clause along with SELECT query and specify an empty String or whitespace (' ') to get empty values instead of \N, e.g.:
SELECT * INTO file
FIELDS ESCAPED BY ''
LINES TERMINATED BY '\n'
FROM test_table;
Here's MySQL's documentation about it, this is what it says:
If the FIELDS ESCAPED BY character is empty, no characters are escaped
and NULL is output as NULL, not \N.
You can update all the columns and set to empty string instead of NULL, If you don't want to continue with NULL.
If you want to continue with NULL in DB, then export into a temp DB table and apply above logic...
First you need to update column default value with below query.
ALTER TABLE `table_name` CHANGE `column_name` `column_name` varchar(length) default '';
UPDATE `table_name` SET `column_name`='' where `column_name`='/N';
May this will help you.
You can use resultset metadata. Iterate through all the columns and check for null values.
ResultSet rsMain = stmt.executeQuery(selectQuery);
ResultSetMetaData rsmd = rsMain.getMetaData();
while(rsMain.next()){
for (int i = 1; i <=rsmd.getColumnCount(); i++) {
if(rsMain.wasNull())
{
sb.append('');// this is the string buffer that writes to the csv
sb.append(',');
}
}
}
You can update the table like this:
UPDATE yourTable
SET col1 = IFNULL(col1, ''),
col2 = IFNULL(col2, ''),
col3 = IFNULL(col3, ''),
...
Once you've done this, you should probably change the schema so that nulls aren't allowed and the default value is '', so you don't have to go through this again.

Substring Binary Data in MYSQL

I have a binary column in my table in mysql. I need to update all the rows for example.
Replace 2nd byte with 1. Doesn't matter what the value was.
SELECT HEX(data) FROM table;
Then
UPDATE table SET data[1] = 1; // the idea
But how do you do something like this in mysql?
Here how you can do it. First get the binary representation with hex function.
Then on the returned value use substring and concat function to replace the 2nd value with 1 and finally update.
Here is an example.
CREATE TABLE t (c BINARY(10));
INSERT INTO t SET c = 'b';
select (HEX(c)) from t;
62000000000000000000
SELECT concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
from t
61000000000000000000
Now putting all together in the update command will replace the value
update t set c = unhex(
concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
);
select (HEX(c)) from t;
61000000000000000000

MySQL Dynamic Table issue

Every month we get a CSV file that contains 300+ columns. The layout of the file can change whenever, and columns can be added/removed. I'm in need of a way to create a dynamic table that contains the exact number of columns needed. I don't care about column names, they can be Column1, Column2, etc.. and I plan to set all types to Varchar(500).
Ideally what I would I want to accomplish is a stored procedure that I can simply pass in the number of columns needed and it will loop to create the necessary Table Definition sql and then execute that sql.
Is accomplishing this even possible? I had started to write the following:
BEGIN
Declare loopvar int default 1;
Declare tsql VarChar(5000);
Declare table_definition VarChar(8000);
Declare tablename varchar(20);
Set table_definition = 'Column';
set tablename = 'npi_data';
Set loopvar = 1;
While loopVar < 362 DO
set tsql = table_definition + loopvar + 'varchar(500) DEFAULT NULL' ;
set loopvar = loopvar + 1;
end while;
set tsql = 'CREATE TABLE' + tablename + '(' + tsql + ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
execute tsql;
END;
I'd like something close to that but all I really need is the ability to create a table with any given number of columns.
Thanks!
If you mean you want to be able to pass a list of column names, like 'A,B,C', and use it to generate a list of column definitions, like A varchar(500) DEFAULT NULL,B varchar(500) DEFAULT NULL,C varchar(500) DEFAULT NULL and, eventually, a CREATE TABLE statement with that definition list, you could use an approach like this:
...
SET columnnames = 'A,B,C';
SET sql = CONCAT(
REPLACE(columnnames, ',', ' varchar(500) DEFAULT NULL,'),
' varchar(500) DEFAULT NULL'
);
SET sql = CONCAT('CREATE TABLE ', tablename, ' (', sql, ')');
...
That is, every comma in the name list is expanded so as to complete the definition of the column preceding the comma. As the last item isn't followed by a comma, the type is simply concatenated one more time to the result of REPLACE.
Alternatively, to avoid the repetition of the varchar(500) DEFAULT NULL bit, you could do this:
...
SET columnnames = 'A,B,C';
SET sql = REPLACE(CONCAT(columnnames, ','), ',', ' varchar(500) DEFAULT NULL,');
SET sql = LEFT(sql, LENGTH(sql) - 1);
SET sql = CONCAT('CREATE TABLE ', tablename, ' (', sql, ')');
...
In this case, a comma is added after the last name, then all commas in the resulting string are replaced like in the previous examples. As a final touch to the list, the trailing comma is removed and finally the complete statement is built.
Either way, no loop is needed.

How do I delete blank rows in Mysql?

I do have a table with more than 100000 data elements, but there are almost 350 blank rows within. How do I delete this blank rows using phpmyadmin? Manually deleting is a tedious task.
The general answer is:
DELETE FROM table_name WHERE some_column = '';
or
DELETE FROM table_name WHERE some_column IS NULL;
See: http://dev.mysql.com/doc/refman/5.0/en/delete.html
More info when you post your tables!~
Also, be sure to do:
SELECT * FROM table_name WHERE some_column = '';
before you delete, so you can see which rows you are deleting! I think in phpMyAdmin you can even just do the select and then "select all" and delete, but I'm not sure. This would be pretty fast, and very safe.
I am doing the mysql operation in command prompt in windows. And the basic queries:
delete * from table_name where column=''
and
delete * from table_name where column='NULL'
doesn't work. I don't know whether it works in phpmyadmin sqlcommand builder. Anyway:
delete * from table_name where column is NULL
works fine.
I have a PHP script that automatically removes empty rows based on column data types.
That allows me to define "emptiness" differently for different column types.
e.g.
table
first_name (varchar) | last_name (varchar) | some_qty ( int ) | other_qty (decimal)
DELETE FROM `table` WHERE
(`first_name` IS NULL OR `first_name` = '')
AND
(`last_name` IS NULL OR `last_name` = '')
AND
(`some_qty` IS NULL OR `some_qty` = 0)
AND
(`other_qty` IS NULL OR `other_qty` = 0)
Since "0" values are meaningless in my system, I count them as empty. But I found out that if you do (first_name = 0) then you will always get true, because strings always == 0 in MySQL. So I tailor the definition of "empty" to the data type.
This procedure will delete any row for all columns that are null ignoring the primary column that may be set as an ID. I hope it helps you.
DELIMITER //
CREATE PROCEDURE DeleteRowsAllColNull(IN tbl VARCHAR(64))
BEGIN
SET #tbl = tbl;
SET SESSION group_concat_max_len = 1000000;
SELECT CONCAT('DELETE FROM `',#tbl,'` WHERE ',(REPLACE(group_concat(concat('`',COLUMN_NAME, '` is NULL')),',',' AND ')),';') FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = #tbl AND COLUMN_KEY NOT LIKE 'PRI' into #delete_all;
PREPARE delete_all FROM #delete_all;
EXECUTE delete_all;
DEALLOCATE PREPARE delete_all;
END //
DELIMITER ;
Execute the procedure like this.
CALL DeleteRowsAllColNull('your table');
I know this has already been answered and has got a tick, but I wrote a small function for doing this, and thought it might be useful to other people.
I call my function with an array so that I can use the same function for different tables.
$tableArray=array("Address", "Email", "Phone"); //This is the column names
$this->deleteBlankLines("tableName",$tableArray);
and here is the function which takes the array and builds the delete string
private function deleteBlankLines($tablename,$columnArray){
$Where="";
foreach($columnArray as $line):
$Where.="(`".$line."`=''||`".$line."` IS NULL) && ";
endforeach;
$Where = rtrim($Where, '&& ');
$query="DELETE FROM `{$tablename}` WHERE ".$Where;
$stmt = $this->db->prepare($query);
$stmt->execute();
}
You can use this function for multiple tables. You just need to send in a different table name and array and it will work.
My function will check for a whole row of empty columns or NULL columns at the same time. If you don't need it to check for NULL then you can remove that part.

MYSQL Truncated incorrect DOUBLE value

When the SQL query below is executed:
UPDATE shop_category
SET name = 'Secolul XVI - XVIII'
AND name_eng = '16th to 18th centuries'
WHERE category_id = 4768
The following error is raised:
1292 - Truncated incorrect DOUBLE value: 'Secolul XVI - XVIII'
How to fix this?
shop_category table structure:
category_id mediumint(8)
name varchar(250)
name_eng varchar(250)
You don't need the AND keyword. Here's the correct syntax of the UPDATE statement:
UPDATE
shop_category
SET
name = 'Secolul XVI - XVIII',
name_eng = '16th to 18th centuries'
WHERE
category_id = 4768
I was getting this exception not because of AND instead of comma, in fact I was having this exception just because I was not using apostrophes in where clause.
Like my query was
update table set coulmn1='something' where column2 in (00012121);
when I changed where clause to where column2 in ('00012121'); then the query worked fine for me.
What it basically is
It's incorrect syntax that causes MySQL to think you're trying to do something with a column or parameter that has the incorrect type "DOUBLE".
Learn from my mistake
In my case I updated the varchar column in a table setting NULL where the value 0 stood. My update query was like this:
UPDATE myTable SET myValue = NULL WHERE myValue = 0;
Now, since the actual type of myValue is VARCHAR(255) this gives the warning:
+---------+------+-----------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'value xyz' |
+---------+------+-----------------------------------------------+
And now myTable is practically empty, because myValue is now NULL for EVERY ROW in the table! How did this happen?
*internal screaming*
Over 30k rows now have missing data.
*internal screaming intensifies*
Thank goodness for backups. I was able to recover all the data.
*internal screaming intensity lowers*
The corrected query is as follows:
UPDATE myTable SET myValue = NULL WHERE myValue = '0';
^^^
Quotation here!
I wish this was more than just a warning so it's less dangerous to forget those quotes.
*End internal screaming*
Try replacing the AND with ,
UPDATE shop_category
SET name = 'Secolul XVI - XVIII', name_eng = '16th to 18th centuries'
WHERE category_id = 4768
The UPDATE Syntax shows comma should be used as the separator.
Mainly invalid query strings will give this warning.
Wrong due to a subtle syntax error (misplaced right parenthesis) when using INSTR function:
INSERT INTO users (user_name) SELECT name FROM site_users WHERE
INSTR(status, 'active'>0);
Correct:
INSERT INTO users (user_name) SELECT name FROM site_users WHERE
INSTR(status, 'active')>0;
I just wasted my time on this and wanted to add an additional case where this error presents itself.
SQL Error (1292): Truncated incorrect DOUBLE value: 'N0003'
Test data
CREATE TABLE `table1 ` (
`value1` VARCHAR(50) NOT NULL
);
INSERT INTO table1 (value1) VALUES ('N0003');
CREATE TABLE `table2 ` (
`value2` VARCHAR(50) NOT NULL
);
INSERT INTO table2 (value2)
SELECT value1
FROM table1
WHERE 1
ORDER BY value1+0
The problem is ORDER BY value1+0 - type casting.
I know that it does not answer the question but this is the first result on Google for this error and it should have other examples where this error presents itself.
It seems mysql handles the type casting gracefully with SELECT statements.
The shop_id field is of type varchar but the select statements works
select * from shops where shop_id = 26244317283;
But when you try updating the fields
update stores set store_url = 'https://test-url.com' where shop_id = 26244317283;
It fails with error Truncated incorrect DOUBLE value: '1t5hxq9'
You need to put the shop_id 26244317283 in quotes '26244317283' for the query to work since the field is of type varchar not int
update stores set store_url = 'https://test-url.com' where shop_id = '26244317283';
1292 - Truncated incorrect DOUBLE value:
This error occurs when you try to compare different types on SQL like `uniqueid` = 1610386969.1713 in this query:
UPDATE `cdr` SET `userfield`='survey=5,' WHERE `uniqueid` = 1610386969.1713
change it for passing the error on this UPDATE example:
UPDATE `cdr` SET `userfield`='survey=5,' WHERE `uniqueid` = '1610386969.1713'
But in your problem, if you change the AND to , the problem will be resolved
UPDATE shop_category SET name = 'Secolul XVI - XVIII', name_eng = '16th to 18th centuries' WHERE category_id = 4768
This is because of "and" in-between while using update query
WRONG ==> "update user_detail set name = ? and phone_code = ? and phone_num = ? and email = ? where emp_code = ?";
instead of this use COMMA(,)
RIGHT ==> "update user_detail set name = ?, phone_code = ?, phone_number = ?, email = ? where emp_code = ?"
If you're getting this problem with an insert that looks like the one below, the problem may simply be the lack of a space between -- and the comment text:
insert into myTable (a, b, c)
values (
123 --something
,345 --something else
,567 --something something else
);
The problem with this is that the --something should actually be -- something with a space.
I experienced this error when using bindParam, and specifying PDO::PARAM_INT where I was actually passing a string. Changing to PDO::PARAM_STR fixed the error.
I did experience this error when I tried doing an WHERE EXIST where the subquery matched 2 columns that accidentially was different types.
The two tables was also different storage engines.
One column was a CHAR (90) and the other was a BIGINT (20).
One table was InnoDB and the other was MEMORY.
Part of query:
[...] AND EXISTS (select objectid from temp_objectids where temp_objectids.objectid = items_raw.objectid );
Changing the column type on the one column from BIGINT to CHAR solved the issue.
// CALL `ut_liquid_name_maildt`() Edit
// Truncated incorrect DOUBLE value: 'IPPAGUNTA VIJAYALAKSHMI'
// Code Sample
BEGIN
-- Declare loop constructs --
DECLARE done INT DEFAULT FALSE;
DECLARE my_id VARCHAR(50);
DECLARE my_name VARCHAR(50);
DECLARE my_mail_dt date;
DECLARE my_name_gl VARCHAR(50);
DECLARE my_mail_dt_gl VARCHAR(50);
-- cursor --
declare cr cursor for select t2.id,t1.name,t1.mail_dt,t2.name as name_gl,t2.mail_dt as mail_dt_gl
from sch_acc_saleint as t1
inner join
sch_acc_salegl as t2
where t1.sch_batch = t2.sch_batch;
-- Declare Continue Handler --
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cr;
read_loop: LOOP
-- Fetch data from cursor --
FETCH cr
INTO my_id,my_name,my_mail_dt,my_name_gl,my_mail_dt_gl;
-- Exit loop if finished --
IF done THEN
LEAVE read_loop;
END IF;
-- Update Query --
UPDATE sch_acc_salegl SET name = my_name and mail_dt = my_mail_dt WHERE id = my_id;
END LOOP read_loop;
CLOSE cr;
END
// I was using wrong update query that"s why it is showing error [ Truncated incorrect DOUBLE value ]
// For this type of error check update query
// For example :
UPDATE sch_acc_salegl SET name = my_name,mail_dt = my_mail_dt WHERE id = my_id;
In my case it was a Dreamweaver function that sanitizes the data before running mysql queries:
GetSQLValueString($my_string, "text")
by mistake I had it as:
GetSQLValueString($my_string, "int")
Basically converting my string to an integer then trying to run MySQL queries based on that. When it should have been a string.
So using "int" instead of "text" caused the problem for me.