I have table with text in it, I want to somehow distinguish in different groups those text that can be found by a LIKE query. So for example I have
blabla
a1aaaa
bla
lab
aaa
a1
c25
I want to get the following result or something similar that I will be able to use easily in my script.
blabla 1
bla 1
lab 1
a1aaaa 2
aaa 2
a1 2
c25 3
Here is the situation
bla IS NOT LIKE '%blabla%'
lab IS NOT LIKE '%blabla%'
blabla IS LIKE '%bla%' So it should go into one group
lab IS NOT LIKE '%bla'
blabla IS LIKE '%lab' Lab should be added to the first created group
Basically I want to group similar words to different groups
Please try the following:
SELECT DISTINCT
A.text_col AS `group_name`,
B.text_col AS `member`
FROM
text_table AS A
INNER JOIN text_table AS B
ON A.text_col LIKE concat( '%', B.text_col, '%')
Basing on your example data, it should give you:
group_name | member
-------------------------------------
blabla blabla
blabla bla
blabla lab
a1aaaa a1aaaa
a1aaaa aaa
a1aaaa a1
c25 c25
I hope it could occur helpful.
Related
I have a table in my MySQL named Animals in which the description column has the values such as
Id
description
age
1
Animal is Cat
14
2
Animal is Dog
3
3
Animal is Tiger
5
4
Animal is Bat
12
5
Animal is Rat
8
6
Animal is Squirrel
13
7
Animal is Cat
4
8
Dog
13
9
Tiger
15
I need to delete those rows which doesn't have the description values Cat, Bat and Squirrel when the description is starting with 'Animal is ' and whose age is above 12 or equal to 12
Which means, I need to only remove the rows whose description only starts with 'Animal is' but I don't want to delete it for 'Animal is cat', 'Animal is Bat', 'Animal is Squirrel'
Id
description
age
1
Animal is Cat
14
4
Animal is Bat
12
6
Animal is Squirrel
13
8
Dog
13
9
Tiger
15
I've tried doing,
DELETE FROM animals WHERE description LIKE 'Animal is%' and Age >=12;
But that seems to be removing all the rows, and I'd like to keep the values of cat, bat and Squirrel.
To select the records you want you can use these conditions:
age >= 12 AND
((description NOT LIKE 'Animal is%') OR (description LIKE '%Cat') OR
(description LIKE '%Bat') OR
(description LIKE '%Squirrel'))
So, you can delete all other rows by applying NOT to the above:
DELETE FROM animals
WHERE
NOT (
age >= 12 AND
((description NOT LIKE 'Animal is%') OR (description LIKE '%Cat') OR
(description LIKE '%Bat') OR (description LIKE '%Squirrel'))
)
Demo here
I am trying to filter the rows having the description that starts with "Animal is" and then, from those rows, I am excluding the results which are specific for cat, bat, squirrel. Note that columns like 'Animal is cat cat' or "Animal is cat bat" or "Animal is cat monkey" would be deleted as well.
DELETE FROM animals where description like 'Animal is%' and description NOT REGEXP '^Animal is Cat|Animal is Bat|Animal is Squirrel$' and age>=12;
Forgive me, I'm still learning but am in need of some assistance. Some of what I’ve done is an amalgam of previous questions but I can’t find quite what I’m looking for.
I have a table with 30 columns of data, let’s call it table1. Every two columns are actually a set of the same type of data that have meaning together and singly. For example col1 with col2, is say a set of names.
Like this:
1 Jim Jeff
2 Mike Ben
3 Mike Mike
4 Peter Jeff
5 Jeff Jim
6 etc etc
The remaining 28 columns aren't important at this point. I want to return a single list of the unique names in col1 AND col2 along with their counts in total from both columns. Here’s what I have and it seems to work to a point but there is a problem with the return.
SELECT col1, COUNT(*)
FROM table1
GROUP BY col1
UNION
SELECT col2, COUNT(*)
FROM table2
GROUP BY col2
The problem is, when col1 has a name in it that is also in col2 it will return two counts. For example, if I had 6 different names, a total of 100 times, 50 in each column I might see something like this returned with the above query.
Jim 4
Jim 13
Jeff 8
Jeff 19
Mike 11
Mike 34
Ben 4
Brian 2
Peter 5
Obviously, Jim, Jeff and Mike appear in both columns and Ben, Brian and Peter appear in only one (It seems to me that it doesn’t matter which one).
What I need returned is:
Jim 17
Jeff 27
Mike 45
Ben 4
Brian 2
Peter 5
I tried putting a subquery in GROUP BY to force what is returned by a union without the count (forgive me, I don’t know much SQL, I'm just making assumptions by what little I understand of the language), meaning:
GROUP BY (SELECT col1 FROM table1 UNION SELECT col2 FROM table2)
but I guess I’m making silly assumptions. Any suggestions?
You can use a CTE to get the list of all names, then do a count based on that.
;WITH Names AS
(
SELECT col1 AS [Name]
FROM table1
UNION ALL
SELECT col2 AS [Name]
FROM table2
)
SELECT [Name], COUNT(*)
FROM Names
GROUP BY [Name]
is it possible to query a result depending on the regex pattern of a column?
assuming i have 2 tables
table A
legs fur name
4 red cat
4 blue || spots dog
1 dolphin
1 black shark
1 yellow shark
1 white|| black whale
2 [0-9]{4}-[0-9]{2} exp1
2 [0-9]{4} expA-1
table B
cageNumber weight legs fur
192910 26 4 red
332192 12 1 black
119199 32 4 blue
111000 19 4 spots
192991 11 4 green
000001 14 2 0913-11
000002 11 2 1102
000003 16 2
what I need to do is to have a select statement which describes cage number and name depending on fur color
cageNumber name
192910 cat
332192 shark
111000 dog
119199 dog
192991 null
000001 exp1
000002 expA-1
000003 null
Change blue || spots to blue|spots to make it a correct regular expression.
Then this should work:
ON B.fur REGEXP CONCAT('^(', A.fur, ')$')
I added ^ and $ in order to anchor to the ends. That way, blue will match, but blueblood will not.
I added ( and ) so that blue|sports would match only blue and sports, not blueblood and teamsports. Think about what happens with ^blue|sports$ -- that means "start with blue or end with sports".
Yes you can do this, look at my little example. It delivers only expertId=1, contingentId=59 and description starts with ## followed by a number.
select * from entry where expert_id=1 and contingent_id=59 and (description REGEXP '^##[0-9]');
SELECT B.cageNumber, A.name FROM A JOIN B ON B.fur LIKE A.fur;
Use join with like
SELECT b.id, a.initiatorWallet FROM ledTest a
INNER JOIN ledTestB b ON a.transtype = b.transType
WHERE b.wallet RLIKE ( a.regex)
I have this table called items and I enabled full text searching on name column
id name price
---- ------ -------
1 brown wood 550
2 black wood 430
3 wooden chair 15
4 kitchen knife 3
5 sponge ball 1.35
I want to write a query to select all items whom name doesn't include 'wood' using full text search
so the result would be
id name price
---- ------ -------
4 kitchen knife 3
5 sponge ball 1.35
here is my query
SELECT * FROM items WHERE Match(name) Against('-wood' IN BOOLEAN MODE);
If you don't want wood, then the fist stab at a query would be:
SELECT *
FROM items
WHERE Match(name) Against('-wood*' IN BOOLEAN MODE);
However, the - only works after other terms are matched. So, you need some sort of positive match. Something like:
SELECT *
FROM items
WHERE Match(name) Against('k* and -wood*' IN BOOLEAN MODE);
If you know that such exclusions are common, you might include a particular word in every name (say, "name"). Alternatively, you might need to resort to like:
select *
from items i
where concat(' ', name' not like '% wood%';
I have two tables, follow and followed. I want to get all the rows in the follow table such that follow.screen != followed.following_screen_name.
follow table
ID screen_name
-----------------
1 eddie
2 jason
3 omar
4 jonathan
5 jack
followed table
ID my_screen_name following_screen_name
-------------------------------------------
1 john eddie
2 kenny eddie
3 kenny omar
4 john jason
5 john omar
Query I tried which didn't work
SELECT follow.screen_name from follow, followed where followed.my_screen_name='john'
AND follow.screen_name != followed.following_screen_name
Expected results
ID screen_name
-----------------
1 jonathan
2 jack
you can get this by doing a LEFT JOIN
SELECT F.screen_name FROM follow F
LEFT JOIN followed FD
on F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
WHERE FD.my_screen_name IS NULL
and FD.following_screen_name IS NULL
Another way is to use NOT EXISTS, get all rows that exists in followed and do NOT EXISTS clause to get desired result.
SELECT F.screen_name FROM follow F
WHERE NOT EXISTS
(
SELECT 1 FROM followed FD
WHERE F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
)
There are plenty of ways to solve this, but common to all is that you need to compare the follow.screen_name to both followed.my_screen_name and followed.following_screen__name.
One way is to use NOT IN with a UNION:
select screen_name
from follow
where screen_name not in (
select following_screen_name
from followed
where following_screen_name is not null
union all
select my_screen_name
from followed
where my_screen_name is not null
)
While this approach is nice for clarity, it may not be as good for performance as using a left join or not exists.
A nice place to pick up mysql syntax and logic is here.
But try this code, it selects every row where the screen_name is not identical to anything produced in the next two queries:
SELECT * from follow WHERE screen_name
not in (select screen_name from followed)
AND not in (select followed_screen_name from followed);
The last two queries would look this and the WHERE filters all of the rows out with screen names identical to the fields below.
my_screen_name following_screen_name
-------------------------------------
john eddie
kenny eddie
kenny omar
john jason
john omar