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)
Related
I would like to get the number of times a fruit is repeated BUT only when the user is different
fruitid
user
fruit
180
217
watermelon
1a6
2dd
apple
1cf
2ac
orange
1da
2dd
orange
1f3
2dd
banana
1a6
2dd
apple
220
1da
banana
254
2dd
apple
2a0
2ac
apple
2a5
229
apple
I tried with this query, but the output is not the expected one for several reasons
SELECT
user,
fruit,
count(fruit) appearancesOfTheFruitAtDifferentUsers
FROM fruitBox
GROUP BY user
HAVING appearancesOfTheFruitAtDifferentUsers > 0
However:
It does not show me how many times a fruit was repeated with a different user in the tables
Suppresses several user-fruit rows; they should all appear with their respective count
user
fruit
appearancesOfTheFruitAtDifferentUsers
1da
banana
1
217
watermelon
1
229
apple
1
2ac
orange
2
2dd
apple
4
I have tried some suggestions from comments, but there was no success.
The output I would like to obtain is:
user
fruit
appearancesOfTheFruitAtDifferentUsers
217
watermelon
1
2dd
apple
3
2ac
orange
2
2dd
orange
2
2dd
banana
2
1da
banana
2
2dd
apple
3
2ac
apple
3
229
apple
3
Here, the count for apple is 3, because it appears for 3 different users. watermelon is 1 because it only appears once. Orange and Banana are each used by 2 users, so their count is 2.
In addition, I would also like to delete row #6 (excluding the table header) as it is a duplicate fruit in the same ID
In a few words, I want the table to show how many times a fruit is repeated in different users and if there is a user with two equal fruits, it only shows one in the table.
If your MySQL version support window function, you can try COUNT window function.
Query #1
SELECT *
FROM (
SELECT user_id,
type,
count(*) OVER(PARTITION BY type ORDER BY type) c
FROM logs
)t1
WHERE c > 1;
user_id
type
c
17ea9b33e6f
signup
5
17ea9c0e9ce
signup
5
17ea9d21366
signup
5
17ea9e04dc7
signup
5
17ea9e04df8
signup
5
17ea27674d1
work
2
17ea27674d8
work
2
View on DB Fiddle
The database term for "different" is distinct, and this term is also an SQL keyword.
Join the table to a subquery that provides the count of distinct users having that fruit:
select
user,
t.fruit,
fruit_user_count
from mytable t
join (
select
fruit,
count(distinct user) fruit_user_count
from mytable
group by 1
) c on c.fruit = t.fruit
See live demo.
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;
I'm in SAS so the code is not pure MySQL.
I have a table like this
ID Fruits
1 Apple-Water-melon
2 Pine-Apple-Kiwi
But also another one with every different fruits
ID Fruits
x Apple
x Kiwi
x Pine-apple
x Water-melon
How can I have a final table like this ?
ID Fruits
1 Apple
1 Water-melon
2 Pine-apple
2 Kiwi
Is it possible to parse the first table and splitting the variable if they match one found in the second table ?
Thanks,
You can use the FINDW() function to check if the FRUIT value appears in the FRUITS list.
data have ;
input ID Fruits $40.;
cards;
1 Apple-Water-melon
2 Pine-Apple-Kiwi
;
data list;
fruitid +1 ;
input fruit $40. ;
cards;
Apple
Kiwi
Pine-apple
Water-melon
;
proc sql ;
create table want as
select a.*,b.*
from have a
left join list b
on 0 ne findw(Fruits,fruit,'-','ti')
;
quit;
Results
Obs ID Fruits fruitid fruit
1 1 Apple-Water-melon 1 Apple
2 2 Pine-Apple-Kiwi 1 Apple
3 2 Pine-Apple-Kiwi 2 Kiwi
4 2 Pine-Apple-Kiwi 3 Pine-apple
5 1 Apple-Water-melon 4 Water-melon
How do you want to eliminate the match for APPLE embedded in the middle of PINE-APPLE? Do you want give priority to multiple word fruit names first and then eliminate those fruit from the fruit list string?
I have a table with this structure:
id seller class ref color pricekg
1 manta apple apple-red red 0.147
2 manta pear pear-green green 0.122
3 poma apple apple-red red 0.111
4 arnie melon melon-green green 0.889
5 pinas pineapple pinneaple-brown brown 0.890
6 gordon apple apple-red red 0.135
I would need to get some fruits from some sellers, with some preferences.
My first objective is to know who sells what im looking for, and after I know that, pick the best one.
When I do the first query I get this:
Query ->
SELECT *
FROM `fruits`
WHERE `seller`
IN ("manta", "poma", "pinas", "gordon")
AND `class` IN ("apple", "pineapple")
ORDER BY id
Result 1 ->
1 manta apple apple-red red 0.147
3 poma apple apple-red red 0.111
5 pinas pineapple pinneaple-brown brown 0.890
6 gordon apple apple-red red 0.135
So far so good, however i get 3 sellers who have red apple's with the apple-red ref.
Now this is the part that i can't resolve...
With this result, I would like to filter the duplicated apples refs ( since i want to buy from one seller ).
If there's duplicates, select the one with the seller manta.
If there's duplicates, and no one of them is seller manta, then select the one with the lowest cost per kilogram.
So after the result 1, the second query (or subquery, or if there's a way to do it all in one query i really don't know what would be the best way) expected result would be:
1 manta apple apple-red red 0.147
5 pinas pineapple pinneaple-brown brown 0.890
Or in case manta didn't sell these it would be:
3 poma apple apple-red red 0.111
5 pinas pineapple pinneaple-brown brown 0.890
Is it possible to do this with only one query?
Or may I somehow do a view from the result or temporal table and then execute one more query to filter the duplicates.
How could I do this?
This is a prioritization query.
I think you want:
select f.*
from fruits f
where f.seller in ('manta', 'poma', 'pinas', 'gordon') and
f.class in ('apple', 'pineapple') and
f.id = (select f2.id
from fruits f2
where f2.seller in ('manta', 'poma', 'pinas', 'gordon') and
f2.class in ('apple', 'pineapple') and
f2.ref = f.ref
order by (f2.seller = 'manta') desc, -- put manta sellers first
f2.price asc -- then order by lowest price
limit 1
);
I need to check in mysql if certain columns contain the same value, but don't actually know the value yet. All the solutions I found until now were using count in combination with a where clause. But that doesn't work for me, because I don't know the values of the colums. For example:
Index ColB ColC ColD ColE
1 1 cat 1.3 black
2 1 cat 1.3 black
3 1 cat 1.3 white
4 1 cat 1.3 tiger
5 1 cat 1.3 white
I would like to check if the 3 columns ColB,ColC and ColD have the same value. For the table above it should return true. However for the following table it should return false
Index ColB ColC ColD ColE
1 1 dog 1.3 black
2 1 cat 1.3 black
3 2 cat 1.3 white
4 1 cat 1.3 tiger
5 1 cat 2.7 white
The rule should be sth like that: if(ColB_hasDifferentValues || ColC_hasDifferentValues || ColD_hasDifferentValues) { return true } ;
Is that possible? As I said before, I don't know which animals are included in ColC, as users can insert new animals.
Thanks a lot in advance!
Just use max() and min():
select (case when max(b) = min(b) and max(c) = min(c) and max(d) = min(d)
then 'same'
else 'different'
end)
from t;
This logic ignores NULL values (the OP does not mention NULL values at all). The idea can be extended, but the logic is a wee bit more complex.