mysql - how to clear some fields content that contains the specific value - mysql

I have a database in mysql. I want clear field contents that contains specific value.
I have database like this:
`id` `cat1` `cat2` `cat3`
1 1185 A1 3185
2 1585 A2 3131
3 2513 B3 3113
4 3369 C4 3333
I want to clear only 'fields' (not entire row) that contain "A" in cat2 column.
how should I do that?
my query doesn't seem to work properly.
instead delete entire row.
DELETE FROM table_name WHERE cat2='A*';

You can use UPDATE to update the value to NULL or an empty string instead of deleting the entire row/column. Use REGEXP to implement regex.
UPDATE EMPLOYEE SET cat2='' WHERE cat2 REGEXP '^A';

Use LIKE:
UPDATE table_name SET cat2 = NULL WHERE cat2 LIKE 'A%';

How about overwrite cat2 with a CASE expression? Verbose but you can clearly see what's going on.
SELECT id, cat1,
CASE
WHEN cat2 NOT LIKE 'A%' THEN cat2
END AS cat2
,cat3
FROM my_table;

Related

Define a custom ORDER BY order in mySQL using the LIKE word

How can I make a custom order where I sort the rows by NAME where the first rows' name has Bob in it followed by rows with name of Alex in it?
To explain what exactly I mean: I have made the following query to sort result if NAME = 'Bob' and if NAME = 'Alex':
SELECT * FROM table
ORDER BY CASE `NAME`
WHEN 'Bob' THEN 1
WHEN 'Alex' THEN 2
ELSE 3
END
But this only works when the NAME is exactly equal to Bob or Alex. I want to modify it to sort if the NAME has Bob or Alex in it, essentially if NAME LIKE '%Bob%' and NAME LIKE '%Alex%'. I tried something like the following but it does not work.
ORDER BY CASE `NAME`
WHEN LIKE '%Bob%' THEN 1
WHEN LIKE '%Alex%' THEN 2
ELSE 3
END
What is the correct syntax for this?
Use the other form of CASE where you specify a condition in WHEN rather than a value.
ORDER BY CASE
WHEN NAME LIKE '%Bob%' THEN 1
WHEN NAME LIKE '%Alex%' THEN 2
ELSE 3
END

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);

SQL query if data match in string from column 2 alter column 22

I have a table with 22 columns.
The particular columns interesting are 2 and 22.
Column 2 has text URLs in each row. I want to to match if URL string contains "this text" then I want to alter column 22 to set number "1" instead of the deafult 0.
I want to do this check for all 600 000 rows in this table. Any idea how to form the query?
A simple Update with like should do:
update your_table
set col_22 = 1
where col_2 like '%this text%';
If you want to make this update only on the rows where column 22 is set to 0 and column 2 contains the text you're looking for, then add another filter:
update your_table
set col_22 = 1
where col_2 like '%this text%'
and col_22 = 0;

Patern maching like Select Query

I have a table that contains this kind a structure of a column, how can I make select only from character 4 to 6 to ignore other character that are outside this boundary , I tried LIKE'%544%', RegExp. ect. ??
1 000544001
2 000054400
3 000544010
4 000344010
5 000544011
One way is to use substr():
where substr(col, 4, 3) = '544'
another is to use like:
where col like '___544%'
you can also use mid(col,start,length) statement
like this
select column1 from table1 where mid(column1,4,3);

Sort MySQL rows by column, but not alphabetically

Sorry if the title is a bit ambiguous and reminiscent of other semi-related questions), the issue is in fact quite simple.
I have a VARCHAR column which can have 1-character values such as M,G,D and S. If I sort the results alphabetically, in this example it will show them in the order: D-G-M-S. However, I need to display the rows in the following order:
G-D-M-S
Is there a way to accomplish this within the query? I know I can custom-sort the results in PHP, but I'd rather do it within the query if possible. For this example, I just need to switch the order of "G" and "D" in the results, and the solution to that simplistic problem will suffice for any answers.
You can write your custom case statement:
Select *
from your_table
order by
case your_column
when 'G' then 1
when 'D' then 2
when 'M' then 3
when 'S' then 4
end
Also, another solution, is to change collation at physical level:
Change default Sorting by Adding a Simple Collation to an 8-Bit Character Set
What I would do is define a temp or permanent table with 2 columns :
letter | ordernum
-------------------
G | 1
D | 2
M | 3
S | 4
Then you join your exiting table to that new one on the field "letter", and use the new table "ordernum" field to do the sort...
SELECT
if(columnname='G',1,
if(columnname='D',2,
if(columnname='M',3,
if(columnname='S',4,0
)))) SortOrder
FROM tablename
ORDER BY
SortOrder ASC
Select col from table
order by case when col = 'G' then 1
when col = 'D' then 2
when col = 'M' then 3
case when col = 'S' then 4 else 5 end ;