Mysql select only unique rows (that appears only one time) - php

Consider following data in table ->
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
...and skip the duplicate values.

The query you want is
SELECT id, email, whatEverColumn
FROM table
WHERE email IN (SELECT email
FROM table
GROUP BY email
HAVING COUNT(id) = 1)

Group by email and add having count(email) = 1

Related

MYSQL count() -> Return the LAST ID from Number of user using the same mail

I have a table named "users". users(id,name,email). My table structure is as follows :
ID is primary key auto increment
I would like to return the email, count and the last_id of user that has the same email address
TABLE :
In other words, my query should return
last_id: 3 - count: 2 - email: jones#jones.com
last_id: 7 - count: 3 - email: silva#gmail.com
I know a piece of the query that returns the count, and the email that is repeated, but I can't get the last id.
SELECT email,count(email) FROM users GROUP BY email HAVING COUNT(email) > 1 ORDER BY count(email) DESC
Add MAX(). Also, for COUNT(email) you can define an alias and use it on HAVING & ORDER BY:
SELECT email, count(email) AS cnt_email , MAX(id) AS latest_id
FROM users GROUP BY email
HAVING cnt_email > 1
ORDER BY cnt_email DESC;
Here's a demo

Multiple Where clause using same columns SQL

I have table like this :
user_id field_id value
1 1 toto
2 1 tata
2 2 tata Job
user_id is the id of my users, field_id is the id of the information about the user (name, job, etc.) and value is the value.
I use a searchBar in HTML/PHP with 2 fields : 'name' and 'job'.
When someone search user using this two fields I need to retrieve the user id that matches these two conditions.
My question is :
How to get in SQL all user_id that match with two condition in one like this:
(field_id=1 and value=tata) AND (field_id=2 and value=tata's Job)
Thank you and have a good day !
SQL executes the WHERE logic for each row, not for the entire set. And fields cannot have more than one value. So, you need to use an OR statement instead of an AND to capture both conditions:
Select User_Id, Field_Id, Value
From YourTable
Where (field_id = 1 And value = 'tata')
Or (field_id = 2 And value = 'tata Job')
One method uses aggregation:
select user_id
from t
where (field_id = 1 and value = 'tata') or (field_id = 2 and value = 'tata''s Job')
group by user_id
having count(distinct field_id) = 2;
You can use IN statement, for example: Your table name is users
SELECT user_id
FROM users
WHERE field_id IN (1,2) AND `value` IN ('tata','tata\'s Job')

Check how many different values there are and return them

Is there a way to check how many rows with different value for ( in this case ) "subject" are.
For example: In my database I have 5 rows.
subject of row 1 = 1
subject of row 2 = 5
subject of row 3 = 3
subject of row 4 = 1
subject of row 5 = 8
I want to have a return like: 1,5,3,8 where as you can notice the 1 is not showed twice.
Does anyone maybe know a way to get this done?
Thanks!
If you only want to return the deduplicated values:
select distinct subject from yourTable;
If you want to know how many times a subject appears in your table:
select subject, count(subject) from yourTable;
If you want to count the unique subject values:
select count(distinct subject) from yourTable;
You have to use the distinct keyword:
select distinct subject from mytable
SELECT * FROM table GROUP BY subject

PHP Mysql Check the row and how many have the same email

I have this problem on PHP MYSQL
this is my database sample
id word email
1 Helo jon#gmail.com
2 Sim jon#gmail.com
3 Sam jon#gmail.com
4 Mac mars#gmail.com
5 Mimic mars#gmail.com
now what i was trying to solve here is that how can i count the email with same email for example
OUTPUT web page :
id word email submitted words dictionary
1 Helo jon#gmail.com 3 regular
2 Sim jon#gmail.com 3 regular
3 Sam jon#gmail.com 3 regular
4 Mac mars#gmail.com 2 regular
5 Mimic mars#gmail.com 2 regular
the submitted words is the count of the email.
how can i work it out on php to count the same email and output 3...
Use this query:
SELECT email, COUNT(*) AS nb_emails
FROM your_table
GROUP BY `email`
It will return for each email in your table the number of associated records.
If you are using mysql_* functions, you may fetch all values using that code:
$sql = "SELECT email, COUNT(*) AS nb_emails
FROM your_table
GROUP BY `email`";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res))
{
// do something with the values of the last fetched record. The values are stored in $row['email'] and $row['nb_emails']
}
Note: Use of the MySQL extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Use mysql_num_rows() For example:
//mysql_connect() & mysql_select_db()
$q=mysql_query("SELECT * FROM tablename WHERE email='jon#gmail.com'");
$count=mysql_num_rows($q); //3
Try this:
SELECT a.ID, a.Word, a.Email, b.SubmittedWords
FROM myTable a INNER JOIN
(
SELECT email, COUNT(*) SubmittedWords
FROM mytable
GROUP BY email
) b
ON a.email = b.email
Use this:
select email,
count(email) as count_same
from table_users
group by email
having ( count(email) > 1 );

How to write mysql5 query for finding duplicate rows from a table?

Hi I have a table like this
ID UserName
1 test#test.com
2 test#test.com
3 john#stack.com
4 test#test.com
5 adam#stack.com
6 john#stack.com
I need an output like this. I need only repeated rows list. How can I create this kind of an output using mysql query.
ID UserName Count
1 test#test.com 3
2 john#stack.com 2
Please help me.
Thanks.
I had the same problem some time ago and solved it like this (as far as I remember):
SELECT *
FROM tableA INNER JOIN
(SELECT DISTINCT MAX(id) as id, type_id, temp FROM tableA GROUP BY type_id, temp) AS t
ON tableA.id = t.id
AND tableA.type_id = t.type_id
AND tableA.temp = t.temp
You join the table with itself selecting the ids that are duplicate. The fields that should be tested against duplicate values are in this case type_id and temp. If you need more or less fields that should be considered as duplicates you can adjust the fields.
I don't know if this helps in your case and if it can be done in a more simple way, so I'm prepared for downvotes ;-)
Edit: removed last condition AND tableA.id < t.id as suggested by ypercube because it leads to 0 results.
It looks like you're trying to pull the following data:
First ID for a given UserName
The UserName itself
The total number of IDs for that UserName
This query should do the trick:
SELECT
MIN(id),
UserName,
COUNT(id)
FROM users
GROUP BY UserName;
since the ID is not unique so its a bit not logical to get the sum of unique UserName from the table.
If the ID is not required we can get the result from single query.
SELECT UserName, COUNT(UserName) AS Count
FROM TableName GROUP BY UserName
HAVING COUNT(UserName) > 1;
But in the case of ID in the result it will be a more complicated query including sub-query and inner table.
SELECT UserName
, COUNT(*) AS `Count`
FROM tableX
GROUP BY UserName
HAVING COUNT(*) > 1
Hi this is the right answer.
SELECT UserName, COUNT(UserName) AS Count
FROM TableName GROUP BY UserName
HAVING COUNT(UserName) > 1;

Categories