Find emails in the same table that having %40 in MySQL - mysql

I have a table with two columns - id, email
What query can I run to show only single emails having %40 ?
e.g. my table is
id email
-------------
1 stevemartin140%40gmail.com
2 stevemartin141%40gmail.com
3 stevemartin140#gmail.com
4 stevemartin141#gmail.com
5 stevemartin148%40gmail.com
6 andymartin%40ymail.com
So result will be:
id email
-------------
5 stevemartin148%40gmail.com
6 andymartin%40ymail.com
i would like to find out & replace those emails with #
so my final output will be:
id email
-------------
1 stevemartin140%40gmail.com
2 stevemartin141%40gmail.com
3 stevemartin140#gmail.com
4 stevemartin141#gmail.com
5 stevemartin148#gmail.com
6 andymartin#ymail.com
Thanks in advance

If you want to replace them, then:
UPDATE t SET email=REPLACE(email, '%40', '#')
-in MySQL.

Maybe not the must performance one but to find duplicate...
SELECT * FROM table GROUP BY REPLACE(email, '%40', '#') HAVING COUNT(*) < 2 AND email LIKE '%\%40%';
MySQL compliant
http://www.sqlfiddle.com/#!2/6808c/3/0

You should use like '%\%40%' in your where condition to escape the wildcard, like this:
select * from t1 where email like '%\%40%';
here is the SqlFiddle

Related

Show records where a value exist in all instances of a field by group

I am trying to figure out a way to show all records in table where a specific field does not contain certain values - table layout is:
id
tenant_id
request_action
request_id
request_status
hash
Each request_id could have multiple actions so it could look like:
1 1 email 1234 1 ffffd9b00cf893297ab737243c2b921c
2 1 email 1234 0 ffffd9b00cf893297ab737243c2b921c
3 1 email 1234 0 ffffd9b00cf893297ab737243c2b921c
4 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
5 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
6 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
7 1 email 1236 1 58c2869bc4cc38acc03038c7bef14023
8 1 email 1236 2 58c2869bc4cc38acc03038c7bef14023
9 1 email 1236 2 58c2869bc4cc38acc03038c7bef14023
Request_id can either be 0 (pending), 1 (sent) or 2 (failed) - I want to find all hashes where all the request_status within that hash are set to 1.
In the above two examples a50ee458c9878190c24cdf218c4ac904 should return as a match as all the request_status are 1 but ffffd9b00cf893297ab737243c2b921c should not as, whilst it contains a 1, it also contains some 0's and 58c2869bc4cc38acc03038c7bef14023 should not as, again whilst it contains a 1, it also contains some 2's
I tried:
SELECT
*
from
table
where request_action='email' and request_status!=0 and request_status!=2
group by hash
However, this doesn't give me the result I need - how can I return the hashes only where request_status is set to 1 for all the instances of that hash?
Not sure why you would need a group by here. You'd want to do a group by if you were going to concat data using GROUP_CONCAT, or other aggregate functions (sum, max, etc)
Also, instead of doing multiple negative conditions in your where clause (request_status !=0 and request_status !=2), why not just get the status you want?
SELECT * FROM test WHERE request_action = 'email' AND request_status = 1
Update Based on Your Comment
If you don't want to return any hashes that have the status of 0, or 2. You can do this:
SELECT
*
FROM
test t
WHERE
request_action = 'email' AND request_status = 1
AND HASH NOT IN (SELECT HASH FROM test WHERE request_status IN (0, 2))
Just make sure you have an index on hash, otherwise this is going to be really slow.
Create table temp select hash from your_table where
request_status=1 group by hash
Alter table temp add index(hash)
Delete from temp where hash IN (select hash from temp
where request_status!=1 group by hash)
Select * from your_table where hash IN(select hash from
temp)

MySQL group by URL path pattern

How to group rows with URLs by path pattern? E.g. we have there addresses:
1 http://example.com
2 http://example.com/products
3 http://example.com/products/some-product
4 http://example.com/categories
5 http://example.com/categories/cat1
6 http://example.com/categories/cat2
7 http://example.com/categories/cat3
8 http://example.com/tags
9 http://example.com/tags/tag1
10 http://example.com/tags/tag2
11 http://example.com/tags/tag3
12 http://example.com/about
So results would be:
1 http://example.com
2 http://example.com/products
3 http://example.com/products/some-product
4 http://example.com/categories
5 http://example.com/categories/cat1
8 http://example.com/tags
9 http://example.com/tags/tag1
12 http://example.com/about
We know domain http://example.com. We need all distinct path types. Basically we want to know what different pages website have. So it's kind of http://example.com/ * / * / * ...
Try this
Rextester Sample
select * from tbl1 t1
where exists
(select 1
from tbl1 t2
group by substring_index(concat(url,'#'),'/',4)
having t1.id=min(t2.id)
);
In MYSQL, there is no hard rule for not selecting columns which are not in group by. So you can go by this as well.
select *
from tbl1
group by
substring_index(concat(url,'#'),'/',4)
order by id
;
Role of substring_index(concat(url,'#'),'/',4)
It will first add an extra character at the end of the url, say #. Then it would truncate the url till the 4th /. Without adding # at the end, http://example.com/categories/cat1 and http://example.com/categories will be put in same group which you dont want.

Query which Find string and increment the count

I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..

How to find records are exist or not from table in mysql table

I want to fetch records from first table & check that records are exist in second table or not:
tbl_user
userid email
-------------------------
1 abc#gmail.com
2 abcd#gmail.com
3 abedd#yahoo.com
4 xyz#gmail.com
5 test#ymail.com
tbl_user_responce
id responce_email
-------------------------
1 abc#gmail.com
2 abcd#gmail.com
3 abc#yahoo.com
4 xyz#gmail.com
5 abcd#ymail.com
UPDATE
Note: In my secord table email is stored in xml format like following:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<userinfo>
<email>stevemartin148#gmail.com</email>
<status>1</status>
<description>Success</description>
</userinfo>
</user>
i want to fetch those records that are not exist in tbl_user_responce from tbl_user
So from above records i want result like
Email
-----
abedd#yahoo.com
test#ymail.com
Does anybody know how to manage that?
Thanks in advance.
How about something like
SELECT *
FROM tbl_user
WHERE NOT EXISTS (
SELECT 1
FROM tbl_user_responce
WHEREN tbl_user.email = tbl_user_responce.responce_email
)
Or even something like
SELECT tbl_user.*
FROM tbl_user LEFT JOIN
tbl_user_responce ON tbl_user.email = tbl_user_responce.responce_email
WHERE tbl_user_responce.responce_email IS NULL
The query you're asking for won't use any indexes, but this would seem to do the job as long as there's only one email per "responce";
SELECT email
FROM tbl_user
WHERE email NOT IN (
SELECT SUBSTRING_INDEX(
SUBSTRING_INDEX(responce_email,'<email>', -1),
'</email>',1)
FROM tbl_user_responce;
)
From the "I'd do an SQLfiddle if it weren't down" dept.

Substring in mysql. Selecting some data

I have a table like
ID Value
A2424 1
A5355 2
A6363 3
A4634 4
AA_A2424 5
AA_A6363 6
I would like to select only those ID's that show up after AA_
so my output should be
ID Value
AA2424 1
AA5355 2
I tried this but it isn't giving me the right output
SELECT *
FROM table
WHERE ID LIKE SUBSTRING( 'AA_', 4, 8 )
Can anyout suggest??
Thanks
Try:
SELECT ID
, Value
FROM table
WHERE SUBSTRING(ID, 1, 3) LIKE 'AA_%'
I guess your output isn't correct for this example table. It should be:
ID Value
A2424 1
A6363 3
Here is a query to get what you need:
select * from AATable where id in
(select substring(id,4,100) from AATable where id like 'AA_%')