How to count and display duplicate entries in MySQL on PHP website - php

I'd like to know how I can count and display duplicate rows on my PHP website. Let's assume that my website allows one to submit a name. How do I then display these names on my website? Let's say that the names in the database are:
John
Derrick
Billy
Jason
Wesley
Billy
John
Billy
I'd then like the website to display the following:
John: 2
Derrick: 1
Billy: 3
Jason: 1
Wesley: 1
How can I achieve this sort of structure?

Yes, you can make this:
select name, count(*) from your_table group by name
And that would return something like this:
name | count
John | 2
If you want the name and the count, you can make a concat
select concat(name, ": ", count(*)) from your_table group by name
And it would return something like this:
name
John: 2
(just one column)

<?php
$ps=$db->prepare("SELECT column_name, count(*) as nb FROM table_name GROUP BY column_name");
$ps->execute()
while($data=$ps->fetch()){
echo $data['column_name'].':'.$data['nb'].'<br/>';
}
?>
$db is the connection instance

Related

How can I count a specific value in a mysql table and echo with php, how many times the varchar is in the table?

I have a MySQL table with several varchars in a column. Now I want to count how many times specific varchar is in this column. Then I want to echo the number that was counted. Can someone tell me what SQL Code I have to use and how I can convert the SQL result to an integer in PHP?
My table:
Let's suppose you have a table with customers data called CUSTOMERS, one of its columns being "NAME".
You could do something like this:
SELECT NAME, COUNT(NAME) AS TOTAL FROM CUSTOMERS GROUP BY NAME
So you will get something like this:
NAME | TOTAL
-----+------
John | 5
Mary | 3
Bort | 9999
If you want to filter by a specific name, you could do something like this:
SELECT NAME, COUNT(NAME) AS TOTAL FROM CUSTOMERS WHERE NAME = 'John' GROUP BY NAME
NAME | TOTAL
-----+------
John | 5
For the second part, it would depend how are you accessing data from your database. For example if you are iterating every row using mysql_fetch_array, and saving the row in a variable $row, you only need to do this:
$name = $row['NAME'];
$total = $row['TOTAL'];

MYSQL Duplicate entries

i am trying to find duplicate entries within my mysql table. I would like to compare the different fields with each other. Here is the structure of my table:
ID FirstName LastName Street ZIP City IpAddress
1 Jack Smith 2nd 12345 Sample1 12.21.24.212
2 Paul Miller 3rd 45685 Sample2 78.54.85.654
3 Jenny Smith 3rd 77273 Sample3 84.91.67.311
4 Frank Jackson 1st 27819 Sample1 78.54.85.654
5 Jack Smith 3rd 72891 Sample2 94.79.99.465
Now i would like to compare the street and ip column individually and then i would like to find the combination of the first- and lastname. There are actually a few more columns in my table that i would like to search for but i think my example above should give you an idea about what i am planning.
I need the id numbers of the entries that could potencially duplicates.
In the example above the output should be the id numbers 1 and 5 when i compare the combination of the first- and lastname.
The output should be the id numbers 2,3 and 5 if i compare the street names.
And the output for the ip addresses should be id numbers 2 and 4.
Does anyone have some ideas about how i should do this? What is the best way to compare those different tables? I don't mind if i have to do several queries.
Use GROUP_CONCAT() to get all the IDs within a group, and GROUP BY to specify the columns that you're looking for duplidates of. And you can use COUNT(*) so you only return the ones that have duplicates.
For streets:
SELECT street, GROUP_CONCAT(id)
FROM yourTable
GROUP BY street
HAVING COUNT(*) > 1
For names:
SELECT firstname, lastname, GROUP_CONCAT(id)
FROM yourTable
GROUP BY firstname, lastname
HAVING COUNT(*) > 1

PHP Mysql - How do I Select top 6 results

Hi I got an search engine where users can search for different names.
The name of the table is "searched_names" and I wanna echo out the first 6 names ordered by most searched. There can example be ten people named Alex in the table and 8 John and 1 Peter then I would like it to echo it out like this.
Alex
John
Peter
SELECT * FROM names WHERE ... guess I'll use COUNT() somewhere
Use ORDER BY and LIMIT
SELECT *
FROM names
WHERE ...
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 6
he wants ordered by most searched.
SELECT * FROM names
ORDER BY col_containing_nb_of_search
limit 6

Partial keyword searching in MySQL

Context:
I am trying to create a search function for my website where a user can type in full sentences and receive results back based on the matching of keywords in the sentence with words stored in a MySQL database:
**ID | Skill**
1 | Painting
2 | Carpenter
3 | Builder
For example a user may search "I want some painting to be done" and using the following MySQL query (along with a foreach and explode function) it will return ID 1 from the database:
$stmt = $mysqli->prepare ("SELECT username FROM users WHERE users.id IN (SELECT
skills.userid FROM skills WHERE skills.skill LIKE CONCAT('%',?,'%') GROUP BY
skills.skill ORDER BY CASE WHEN skills.skill LIKE CONCAT(?,'%') THEN 0 WHEN
skills.skill LIKE CONCAT('% %',?,'% %') THEN 1 WHEN skills.skill LIKE CONCAT('%',?)
THEN 2 ELSE 3 END, skills.skill)");
Exam question:
The issue I have is that if a user was to type "I want a painter" then ID 1 would not be returned. How can the query be modified to account for the fact that painting and painter are similar and so should be returned?
You can add to skills table a column called synonymous with some keywords for that skill.
For example, the "Painting" row will have a "paint painting paintor" in synonumous column.
Then you change your query to check for synonymous column insted of skill column.
This is the simples way, but requires that you put a synonymous to each skills table row.

select statement without including duplicate data

I am trying to select from a database but because the database as duplicate data and the item names in each data may or may not be duplicated. Please look at the example below to understand more
Table shoe
shoeid pid Name
1 1 green
2 1 green
3 2 red
4 3 red
Thats a simple example.
How I can I select Name and pid from this table but i dont want to see any repeated numbers or names. for example i don't want to see red or green or whatever color i have in the database more than once. Please remember I have over 100 colors in the database. The same thing apply to the pid
Use DISTINCT
http://www.mysqltutorial.org/mysql-distinct.aspx
http://www.w3schools.com/sql/sql_distinct.asp
This might be what you want
this gives you only unique results
SELECT DISTINCT Name, pid FROM shoe;
try this:
SELECT DISTINCT Name FROM table_name;
or if you want maximum id in output:
SELECT Name, MAX(pid) AS id FROM table_name GROUP BY Name;
or if you want comma separated list of ids for that Name:
SELECT Name, GROUP_CONCAT(pid) AS id_list FROM table_name GROUP BY Name;
Use GROUP BY
select * from `yourtable` group by `pid`,`Name`
SELECT DISTINCT Name FROM shoe
this query get the unique values , if you want where you can use it by
SELECT DISTINCT Name FROM shoe WHERE your_key = 'your_key_val'

Categories