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 );
Related
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
My table for message inbox concept email is a primary in the table. I want to get the last message which is sent by particular email.
tbl_msg:
id
email
msg
Example Data:
Id email msg
1 xyz#gmail.com This is test
2 abc#gmail.com All is well
3 xyz#gmail.com This is test2
I want to get the last appearance of each email and msg
Id email msg
2 abc#gmail.com All is well
3 xyz#gmail.com This is test2
What I tried:
SELECT cnote.`id`,cnote.`email`,cnote.`msg` FROM `tbl_msg` cnote inner join (select distinct email,id from client_com group by email) as note
on cnote.id=note.id
Guide me if I wrong
Use This query:
SELECT m1.* FROM tbl_msgs m1 LEFT JOIN tbl_msgs m2 ON (m1.email = m2.email AND m1.id < m2.id) WHERE m2.id IS NULL;
This will join with the same table and with condition m1.id < m2.id , will get the latest one.
Another option is using subquery:
SELECT * FROM tbl_msgs WHERE id IN (SELECT MAX(id) FROM tbl_msgs GROUP BY email);
To get the latest messsage for each email address in the table you do not need a JOIN, you just need to ORDER and GROUP BY:
SELECT `id`, `email`, `msg`
FROM `tbl_msg`
GROUP BY `email`
ORDER BY `id` DESC
You can use this query:
SELECT id, email, msg FROM you_table
GROUP BY email
ORDER BY id DESC
With this one you will have only 1 row per email, and you'll have the last because is ordered by id DESC.
If you are running a PDO connection (The way I know to do it, not sure if there is a MySQLi way)
$theLastIdGiven = $handlerDbConnection->lastInsertId();
Or if you want to run it through a query, and get the last result, just add ORDER BY id DESC LIMIT 0, 1 and that will order the data by id in a descending form then get the first bit of data :)
Hope this helped you!
SELECT ID, email, Msg FROM tbl_msg WHERE ID IN (
(SELECT MAX(ID) FROM tbl_msg GROUP BY email))
I have table with files and their have their points/score. Iam trying to summarize those points and print it. I have a where clause for id files, but if I select random files it has same score as others like where clause isnt working.
$id is correct for each file.
This is my query:
$result = mysql_query("SELECT *,SUM(body) FROM soubory, users, hodnoti WHERE
soubory.id='".$id."' AND soubory.users_id = users.id");
hodnoti table
users_ID soubory_id body
14 44 7
15 44 9
And now, if there is no record (in table hodnoti) for soubory_id = 45 the result is same as for 44 despite of where clause.
users table
id nick
14 user1
15 user2
soubory table
id nazev users_id
44 file1 14
45 file2 14
That query should help me print this:
Who uploaded, nazev(title) of the file and then points. But if file2 has no record in table hodnoti, still has score of file1. Hope it helps.
$sql = "SELECT *, SUM(hodnoti.body) as sum FROM soubory
INNER JOIN users ON soubory.users_id = users.id
INNER JOIN hodnoti ON soubory.id = hodnoti.soubory_id
WHERE soubory.id='".$id."' ";
$result = mysql_query($sql);
This query works for me. When i set id=44 and get a result and when set id=45 and get all null value. i have attached two result image. I have just create table and insert your given data and run query on phpmyadmin sql tab query box. You can try this for check your query works or not.
Use JOIN.
$sql = "SELECT *, SUM(hodnoti.body) as sum FROM soubory
INNER JOIN users ON soubory.users_id = users.id
INNER JOIN hodnoti ON soubory.id = hodnoti.soubory_id
WHERE soubory.id='".$id."' ";
$result = mysql_query($sql);
SUGGESTION: Above is a direct answer for your question. Avoid using mysql_* statements as they are deprecated now. Learn mysqli_* or PDO and start implementing that.
Okay so I have a table that has the following
KEY username password score
The above columns are not in any specific order.
I want to send my Database a username and have it send me back what rank that user name is based on its score. So for example if I had 10 people in there and the 3rd person in has the highest score. When I pass the 3rd persons username in I want it to send back 1.
Is this possible?
I have been trying things like this
$result = mysql_query("SELECT * FROM tablename where username='$username' ORDER BY score DESC");
but it doesnt seem to give me the row number
This will handle ranks that have the same score.
SELECT d.*, c.ranks
FROM
(
SELECT Score, #rank:=#rank+1 Ranks
FROM
(
SELECT DISTINCT Score
FROM tableName a
ORDER BY score DESC
) t, (SELECT #rank:= 0) r
) c
INNER JOIN tableName d
ON c.score = d.score
// WHERE d.username = 'Helen'
SQLFiddle Demo (Ranking with duplicates)
SQLFiddle Demo (with filtering)
for example
KEY username password score Ranks
1 Anna 123 5 3
2 Bobby 345 6 2
3 Helen 678 6 2
4 Jon 567 2 4
5 Arthur ddd 8 1
for better performance, add an INDEX on column Score,
ALTER TABLE tableName ADD INDEX (Score)
SELECT
(SELECT COUNT(*)+1 FROM tablename WHERE score > t.score) as rank,
*
FROM
tablename t
where
username='$username'
The ORDER BY in your query is useless since you're only returning one row.
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;