Delete a specific value from a string (string contains comma separated values) - mysql

For example, if I have a table structure like this:
Table 1
ID Name Value
001 Rajesh 90,100,210,400
002 Suresh 100,400,300,66
003 Mahesh 200,500
004 Virat 400,578,57
How can I delete 400 from Suresh?
DELETE Value ="400" FROM table1
WHERE Name = 'Suresh'
This doesn't work.

I would recommend splitting the values into a second table which is related via the person's ID. However, you can use the following query for your current situation:
UPDATE table1
SET Value = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', Value, ','), ',400,', ','))
WHERE Name = 'Suresh'
Here's a SQL Fiddle. For reference, see MySQL's string functions

Related

I need to display the data, if at least one string present in another column using SQL

I have two columns in a table, column 1 contains tom jerry and column 2 has tom xxxx . I need to fetch the row at least one string is present the other column data
column1 column2
-----------------------------
tom jerry : tom xxx
tiger : tom yyy
tiger lion : lipard
lion tom : tiger lion
23 : 235 452
23 : 23
Expected result:
column1 column2
---------------------------
tom jerry : tom xxx
lion tom : tiger lion
23 : 23
Here is my approach:
Put both columns to another table with identity column (to identify each row)
Write a function to split text by a string (in this case pass a space)
Here you can use built in function if it is higher than SQL Server 2016
Get the identity and the splitted data to 2 tables each for column 1 and 2
Join the 2 tables for matches in Col-Split data, and get the IDs
Now Query for the data in table in above 1
Here is the Fiddle assuming that the code is for SQL Server 2016
In MySQL, you can use a regular expression for the data you have presented:
where column1 regexp replace(column2, ' ', '|') or
column2 regexp replace(column1, ' ', '|')
This may or may not work as stated. For instance, this will match "lion" and "lions" -- that meets what you are describing as I read it. That said, you can modify the patterns to enforce word breaks if you have something slightly different in mind.
You'd be able to achieve this by creating a function wherein you will pass value of column1 and column2. The function will do the heavy-lifting of checking if column1 value (multiple words separated by whitespace) has any of its word matching in column2 value. The pseudo code of function should be something like this -
Function bool IsCol1WordPresentInCol2(varchar2 col1value, varchar2 col2value)
Step 1: Split col1value based on whitespace to get series of words
Step 2: Split col2value based on whitespace to get series of words
Step 3: Outer loop to iterate through all words of col1 (extracted from step 1)
Step 4: Inner loop to iterate through all words of col2 (extracted from step 2)
Step 5: Check if the col1 and col2 words do match. At any point, if any match is found, return true from the function; else false.
Step 6: Based on the returned bool value from this function, you'd be able to identify, in your calling logic, if that record satisfies your condition.
You can use instring function to achieve this. Function depends on the database you use. Below is the query,
select * from table1 where col1 > col2;
select distinct t1.* from table1 t1
inner join table1 t2
on (instr(substring(t1.col1, instr(t1.col1, ' ') -1), col2) > 0);

partial search a dash value in a field in mysql

my mysql table name is: student having 3 fields id(pk, auto increment), name(varchar 200) and student_id(varchar 200)
values are lik
id name student_id
1 abc 123-234
2 efg 2345678
3 xyz 1234-9
4. pxy 323-289
now i want to select those students whose id like 3-2
SELECT * FROM student
WHERE student_id like '%3-2%'
unfortunately the query return zero result.
any help is highly appreciable.
SELECT * FROM student
WHERE student_id like '%3-2%'
This works alright when I tested on MySql using PhpMyadmin.
If you use some other SQL, then try replacing single quotes to double quotes (' ' to " ").
Try firing the following query.
SELECT * FROM student
WHERE student_id like "%3-2%"

Advanced MySQL pattern matching (beyond LIKE...%)

This is the example of my current MySQL my_table...
id name code
1 111 XXXX123456XXXXXXXXXXXXXX
2 222 XXXX133456XXXXXXX5XXXXXX
3 333 XXXX123454XXX11XXXXXXABC
Code is a 24 character hexadecimal value where X is the wildcard.
I need to write a query that will return the NAME based on the CODE without a wildcard value X.
The given CODE value will be exact but it should compare the string in place, X could match any character.
For example:
SELECT name FROM my_table where code = '012312345611111111111111';
name
111
SELECT name FROM my_table where code = '000013345622222225123456';
name
222
SELECT name FROM my_table where code = '000123454ABC11234567FABC';
name
333
You can use like for this. Are you aware of the _ wildcard?
select t.*
from t
where #YourCode like replace(t.code, 'X', '_');
Of course, you can use regular expressions too. The regular expression would be: concat('^', replace(t.code, 'X', '.'), '$').

how to write sql query using substring

I have some fields in a table Employee, with fields
empID,empName,empAddress,empCity...
And all the values of empAddress field contains like "ADR 250 Candy", "ADR 330 Simla, "ADR 220 Karty" and so on.
I want a query to list otr the address without the word 'ADR'.
Eg: I want to display the address like "250 Candy", "330 Simla", "220 Karty" ....
Please help me on this
Try
SELECT SUBSTR(E.empAddress, 4) AS 'Address' FROM Employee E
If you want to skip the space character also replace SUBSTR(E.empAddress, 4) with SUBSTR(E.empAddress, 5)
try this :
SELECT EmpAddress = REPLACE(E.empAddress, 'ADR ', '')
FROM Employee E

Mysql comma count from field value

I want to count string separators from a MySQL query, mean if the field value is
like :-
1,2,3,4,5
as the string is comma separated so the separator count will be 4.
any idea then please share
THANKS,
you can try to count the length of string and minus the length of string without commas as follows:
LENGTH('1,2,3,4,5') - LENGTH(REPLACE('1,2,3,4,5', ',', ''))
select length('1,2,3,4,5') - length(replace('1,2,3,4,5', ',', ''))
I suggest the following design :
Table name : USER_HOBBIES
| USER_ID | HOBBY_ID |
1 1
1 2
1 3
2 2
2 4
2 5
And now you can easily count user hobbies for a given user :
SELECT count(*) FROM USER_HOBBIES WHERE USER_ID = <user-id>
although it requires another table it is much clearer and on a long list of hobbies this will be much faster than using a function for manipulating strings.