Dan Andrews
Dan Andrews

Reputation: 17

Count total rows with same value, and echo the value of the row value

I am working on something for my place of employment, and I want to be able to show the employee the "most favorite color" ..

When an employee uploads using the PHP CRUD I am developing, they can enter

White, Yellow, Black, Green, Red, Orange, Brown, Purple, Pink, Grey, Blue as part of the "batch control" system.

What I am wanting to do, is count the total rows containing whatever color it is, and echo out the "most used color"

My table:

<tr>
                                    <th>#</th>
                                    <th>Date</th>
                                    <th>TOS</th>
                                    <th>Tag Batch #1</th>
                                    <th>Tag Batch #2</th>
                                    <th>Colour</th>
                                    <th>User</th>
                                    <th>Wastage</th>
                                    <th>Reason</th>
                                    <th>Laser</th>
                                    <th>Options</th>
                                </tr>

Above all of that on my index page is a

<div class="media-body">
                                        <h5 class="md-title nomargin">Most Favorite Colour</h5>
                                        <h1 class="mt5">###</h1>
                                    </div><!-- media-body -->

How would I go about showing the user what the most uploaded color is?

My database table is like the following;

ID | Date | TOS | tags1 | tags2 | colour | user | wastage | reason | comments

An example output would be

2021-06-02
500110
169700
Pink
My_Name
4
2021-06-30 18:39:2```

Upvotes: 1

Views: 119

Answers (1)

Priyanka Pareek
Priyanka Pareek

Reputation: 26

SELECT t.*
FROM (
    SELECT COUNT(ID) as totalcount, 
           colour
    FROM Colors
    GROUP BY colour) t 
ORDER BY totalcount DESC LIMIT 1;

This query will fetch most inserted color in table with count, so you can use it.Use your table fields and table name proper in above query.

Upvotes: 1

Related Questions