Mysql only do not select duplicated values - mysql

I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
1 example#hotmail.com
2 example12#hotmail.com
...and skip the duplicate values.

SELECT MIN(id), email FROM some_table GROUP BY email

SELECT DISTINCT email FROM table

If you don't need ID use
SELECT DISTINCT email FROM `TABLE_NAME`
else If you need the First ID use
SELECT MIN(ID),email FROM `TABLE_NAME` GROUP BY email

There are several ways to accomplish this, one is to use the DISTINCT clause:
SELECT DISTINCT email FROM your_table;
another way is to summarize counts of the values:
SELECT COUNT (*), email from your_table GROUP BY email;

SELECT DISTINCT UNIQUE_FEILD_NAME FROM YOUR_TABLE_NAME

Related

Get data from two tables using IN clause

What i want to get?
Get id,name,email of the people who have sent me friend_requests.
Two tables i have used on the basis of which i want to get the details
signup -> id,name,email,firmname and a few more columns.
friends_requests ->id,userId,sentRequests,receivedRequests,friends,dates
Please refer to the below two images.
friends requests table
Signup table
What I am trying?
SELECT * FROM signup WHERE signup.id IN
(SELECT sentRequests FROM friends_request WHERE friends_request.userId=46)
This query only gives one record, whereas if i use below query it gives 3 records
SELECT * FROM signup WHERE signup.id IN (47,48,49)
I know why second query is giving three records because of the IN clause and three id's
But this query will also give the same result which is 47,48,49
SELECT sentRequests FROM friends_request WHERE friends_request.userId=46
But why isn't the first query giving three records?
When both the values are same? Then why isn't the result same?
About table friends_requests. Column sentRequests violates 1NF.
if I am right, IN operator gets your information as full string, not as separate ID information.
SELECT *
FROM signup
WHERE signup.id IN ('47,48,49')
what is operator thinks IN ('47,48,49')
what You think IN (47,48,49)
You have to split column value '47,48,49' into returning rows so operator could understand, if you want to use IN operator...
For it to work like you coded it,
SELECT sentRequests FROM friends_request WHERE friends_request.userId=46
would have to give 3 rows with 47,48,49.
Because sentRequests is a string it gives one row with 47,48,49
If you want the 3 values go with:
select * from #signup a where exists (select 1 from friends_request b where ','+sentRequests+',' like ',%'+cast(id as varchar(5))+',%')
try this...
SELECT * FROM signup WHERE id IN (
SELECT
DISTINCT cast(SUBSTRING_INDEX(SUBSTRING_INDEX(sentRequests, ',', n.digit+1), ',', -1) AS signed) sentRequests
FROM friends_request INNER JOIN (
SELECT
0 digit
UNION ALL
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
) n ON LENGTH(REPLACE(sentRequests, ',' , '')) <= LENGTH(sentRequests)-n.digit
WHERE userId = 46
);

only do not select duplicated values mysql?

I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
2 example12#hotmail.com
Use aggregation count(*) and check the result of aggregate function using having clause to filter out those records which are not duplicated
select *
from demo
group by email
having count(*) = 1
Demo
select id,email from table group by email having count(*) =1;

SQL query / number of records

I have a simple table with two columns. The first field represents an id, the second one a name (string value). No field is unique. So, e.g., there are many records like:
What I need is a simple SQL statement which shows me the whole table in the following format and inserts the content into a new table.
Any help?
You have to use group by:
insert into tab2 (name,cnt)
select name, count(1) as cnt
from tab
group by name
Here is more informations about aggregate functions.
SQL Fiddle DEMO
Just use COUNT function to count from_id. And if you want to count in group, use GROUP BY clause for that. Like this one.
SELECT `name`, COUNT(from_id) AS `COUNT`
FROM myTable
GROUP BY `name`;
This will return your desired result.
Now you want to insert it into new table then you can do like this:
INSERT INTO newTable (`name`, `count`)
SELECT `name`, COUNT(from_id) AS `COUNT`
FROM myTable
GROUP BY `name`;

how to get the exact row using query

I want to get the exact row from the following data
id name groupid
1 robert 1,2
2 henry 11,12
My query is
SELECT * FROM table WHERE groupid LIKE '%1%'
Above query will return both row
How to get the first row ?
You could use FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('1', groupid)
But as suggestion, you should not save data like this.
assuming groupid is a varchar column having ids stored as comma seperated list you can try this:
select * from table where CONCAT(',',groupid,',') LIKE '%,1,%';
or better approach would be to use FIND_IN_SET function in mysql:
select * from table where find_in_set(1, groupid);
To get an exact row from a database table, just specify all the fields:
SELECT *
FROM myTable
WHERE id = 1
AND name = 'robert'
AND groupid = '1,2'
Or, assuming id is the unique primary key, you can just use that:
SELECT *
FROM myTable
WHERE id = 1
Using LIKE '%1%' will return all the results which contain 1. Use #lc solution WHERE groupid = '1,2' to get only result with that specific id.

Find duplicate strings in database

I need to find all rows in my table where the strings of a specific field are duplicates in two or more places.
Can that be done in a MySQL statment?
EDIT
I need to get every row not just a count of how many duplicates there are. I want to be able to edit the fields.
Yes, try something like this:
SELECT *
FROM `YourTable`
WHERE `YourColumn` IN (
SELECT `YourColumn`
FROM `YourTable`
GROUP BY `YourColumn`
HAVING COUNT(*) > 1
)
Yes, using GROUP BY and HAVING.
SELECT mycolumn, count(*) FROM mytable
group by mycolumn
having count(*) > 1