MySQL #1241 - Operand should contain 1 column(s) - mysql

I want to update the column 'page_inv', so it will replace two last chars of this column, and add new data, for example:
'{"userItems":[{item1}]}' -> '{"userItems":[{item1}' -> '{"userItems":[{item1}, {item2}]}'
UPDATE `users`
SET `page_inv`=CONCAT((SELECT `page_inv`, SUBSTRING( `page_inv`, 1, CHAR_LENGTH( `page_inv` )) -2), ',{newItem}]}')
WHERE `STEAMID`=76561198147
My DB:

Something like this?
UPDATE `users`
SET page_inv= CONCAT(left(page_inv, length(page_inv) - 2), ',{newItem}]}')
WHERE STEAMID = 76561198147;

Related

Split a column into 2 columns mysql table

I have this column in mysql table:
LOT_LOCATION
SGBAKE.0013
SGHAST.0008Z1
SGHAST.0011ZU
How to split to this table[MANAGED TO DO SO BUT DK HOW TO CHANGE THE TABLE ITSELF):
LOT_LOCATION, Zone Attribute
SGBAKE.0013, ''
SGHAST.0008, Z1
SGHAST.0011, ZU
Any help is appreciated thanks!
my code only select 2 columns but does not alter the table and I dont know how to put condition in creation and alteration of columns:
select if(locate('Z',LOT_LOCATION)=0,LOT_LOCATION,substring_index(LOT_LOCATION, 'Z', 1)),
if(locate('Z',LOT_LOCATION)=0,'',substring_index(LOT_LOCATION, 'Z', -1))
As Zone_Attribute
from skynet_msa.Lab_WIP_History;
I tried this UPDATE but suddenly the zone attribute column values disappear
UPDATE Lab_WIP_History
SET LOT_LOCATION = if(locate('Z',LOT_LOCATION)=0,LOT_LOCATION,substring_index(LOT_LOCATION, 'Z', 1)),
`Zone Attribute` = if(locate('Z',LOT_LOCATION)=0,'',substring_index(LOT_LOCATION, 'Z', -1))
lot_location is updated before zone_attribute - ie the zone_attribute test finds no z in lot_location btw your select query does not produce the result you claim
reverse the order of the set statement
UPDATE Lab_WIP_History
SET `Zone Attribute` = if(locate('Z',LOT_LOCATION)=0,'',substring_index(LOT_LOCATION, 'Z', -1)),
LOT_LOCATION = if(locate('Z',LOT_LOCATION)=0,LOT_LOCATION,substring_index(LOT_LOCATION, 'Z', 1))
;

SQL: How can I make a query to get the STRING that do not contain 3 letters 'r'?

Example:
wordrr -> Incorrect because it have 3 'r'
word --> Correct becouse it not have 3 'r'
You want to fetch those rows in which a specified column doesn't contain the character "r" 3 times.
Here is the code to count the occurrence of character "r".
SELECT (LENGTH(ColName) - LENGTH(REPLACE(ColName, 'r', ''))) as "R Count" from TableName;
Note: If LENGTH doesn't work, try using LEN.
Now in order to fetch rows which doesn't have character "r" occurring 3 times or more
SELECT * from TableName where (LENGTH(ColName) - LENGTH(REPLACE(ColName, 'r', ''))) < 3 ;
Also you can use:
SELECT * from TableName where (char_length(ColName) - char_length (REPLACE (ColName, 'r', ''))) <= 3 ;

I am struggling to remove the last 30 characters from data in a column

I am looking to remove the last 30 characters from a colum within my database.
Column Name = gatewayid
Table Name = tblclients
I have created the below which works in the sense it shows me the result and it's correct but it does not commit or change anything.
SELECT gatewayid, /* ANSI Syntax */SUBSTRING( gatewayid
FROM 1
FOR CHAR_LENGTH( gatewayid ) -30 ) AS col_trimmed, /* MySQL Syntax */SUBSTRING( gatewayid, 1, CHAR_LENGTH( gatewayid ) -30 ) AS col_trimmed
FROM tblclients
What am I missing, I am a noob :)
I expect the data in the column to remove the last 30 characters from each row.
You need to use update statement:
update tblclients
set gatewayid = substring( gatewayid, 1, char_length( gatewayid ) -30 );
You could use
SELECT gatewayid
,/*ANSI Syntax*/ SUBSTRING(gatewayid FROM 1 FOR CHAR_LENGTH(gatewayid) - 30) col_trimmed_ansi
,/* MySQL Syntax*/ SUBSTRING(gatewayid, 1 CHAR_LENGTH(gatewayid) -30) AS col_trimmed_mysql
FROM tblclients
and for update the table content
UPDATE tblclients
set gatewayid = SUBSTRING(gatewayid FROM 1 FOR CHAR_LENGTH(gatewayid) - 30)

MySql search integer range from number with dash

I have table in that I have one field with dash value. Like...
I need to search this with between condition.
For example if I have one value 25 then I need to search the records which include the value 25 like 20-31. In above image there are 6 records which include 25 value. So it should return 6 records.
Please help me in this query ? What would be the query for that ?
You can use MySQL's substring_index() function to easily get the data before and after the dash:
select substring_index(yourcolumn,'-',1) as `lower`, substring_index(yourcolumn,'-',-1) as `upper`
from yourtable
This way you can return the records where a certain value falls between the range:
select * from yourtable
where 25 between substring_index(yourcolumn,'-',1) + 0 and substring_index(yourcolumn,'-',-1) + 0
The + 0 forces MySQL to convert the result of substring_index() to a numeric value before the comparison.
You can use the following solution using SUBSTRING_INDEX:
SELECT *
FROM table_name
WHERE 25 >= CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 1), '-', -1), UNSIGNED INTEGER)
AND 25 <= CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 2), '-', -1), UNSIGNED INTEGER)
-- or
SELECT *
FROM table_name
WHERE 25 BETWEEN CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 1), '-', -1), UNSIGNED INTEGER)
AND CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 2), '-', -1), UNSIGNED INTEGER)
demo: http://sqlfiddle.com/#!9/4ac7b3/3/0
I recommend you to change your table design. I would split the column using the VARCHAR datatype to two columns using the INTEGER datatype. You can add two new columns with the the following ALTER TABLE commands:
ALTER TABLE table_name ADD colNameA INT;
ALTER TABLE table_name ADD colNameB INT;
To split the values of you current column and update the values to the new columns you can use the following UPDATE command:
UPDATE table_name SET
colNameA = CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 1), '-', -1), UNSIGNED INTEGER),
colNameB = CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 2), '-', -1), UNSIGNED INTEGER)
At the end you can remove the VARCHAR column using this ALTER TABLE command:
ALTER TABLE table_name DROP COLUMN col_name
Now you can use the following (simple) query to get the expected results:
SELECT *
FROM table_name
WHERE 25 >= colNameA AND 25 <= colNameB
-- or
SELECT *
FROM table_name
WHERE 25 BETWEEN colNameA AND colNameB
If you want to get values beween 35 and 39, you can use below query,
SELECT
*
FROM
yourtable
WHERE
35 && 39
BETWEEN SUBSTRING_INDEX(tablecolumn, '-', 1) + 0 AND
SUBSTRING_INDEX(tablecolumn, '-', - 1) + 0
I don't know how it possible with MySQL.
But using php it possible to check with range.
For e.g.
// First of all get all record from database.
$search = 10; // Your searching value.
// Loop all rows.
while($rows = mysqli_fetch_array($r)){
$explode = explode("-",$rows['dash']); // For get from-to value.
$range = isset($explode[0])&&isset($explode[1])?range($explode[0],($explode[1]-1)):array(); // For get range.
if(in_array($search,$range)){ // For check searching value is exist or not !
echo "Yes ! I get into ".$rows['dash']; // Do stuff.
}
}
Note: If 10-15 then it will check with 10,11,12,13,14.
According to me if you dont want to change the table structure then,
Just fetch the records as per your other condition, Then from that data check your amount between that field using foreach loop and explode. like
If you have $data as all data
foreach($data as $value){
$new_val=explode(',',$value['new_field']);
if(25 >= $new_val[0] && 25 <= $new_val[1]){
// here create new array
}
}

MySql: updating a column with the column's content plus something else

I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'