How to Show Top Value filed as top in Mysql - php

I want to show the college name and num of applications comes to each college and show most application received college as first position in MySQL query?
This is sample table, clg is college name and application_name column must be count() and highest value field must be show on top
My results must be like this....

try this,
SELECT `clg`,count(`clg`) AS clg
FROM demo
GROUP BY `clg`
ORDER BY clg DESC

I am answering this because the columns in a select should be unique, particularly when they are referenced in the order by clause. The query should look more like:
SELECT s.clg, count(*) as cnt
FROM sample s
GROUP BY s.clg
ORDER BY cnt DESC ;

Related

MySQL & PHP - Best way to get a list of descending count of countries from database table

I am trying to compile (to display in a web page) a list of countries and the counts from a MySQL DB. The entries are in a table and I need to display on a page the list of countries in the DB, in order of how many entries there are.
So I know how to get a list of countries from the database with, for example,
SELECT COUNT(*) FROM table_name WHERE country_code='GB'
And I know how to get a list of countries with, for example,
SELECT DISTINCT country_code FROM table_name;
But I am struggling to put it all together and get a list of only the countries that are actually in the DB, then their count and then put them in descending order.
Looking for a gentle nudge in the right direction to find out how to do this.
SELECT
COUNT(*),
country_code
FROM table_name
GROUP BY country_code
GROUP BY is what you need for this type of problem.
You can add an ORDER BY to the end of the query, if necessary.
Try:
select count (*), country_code from (SELECT DISTINCT country_code FROM table_name)
But the proper way is using GROUP BY
select count(*), country_code
from table_name
group by country_code
To get them ordered by date for ex, you do:
select count (*), country_code from
(SELECT DISTINCT country_code
FROM table_name
order by date
)
Or (by using group by):
select count(*), country_code
from table_name
group by country_code
order by date

SQL Select the column if the value is like with multiple columns

I need to create a autocomplete search with ajax. The suggestions should only contain the 10 most entered results. The search query has to check multiple columns if the value is like my variable.
But my problem is to create the query and the php logic for that.
Is there any plugin or something simular for that?
How can I select a column if the value in it is like my variable?
I need to create a count query, which counts (in all columns) "how often is here the full word (splitted by spaces)" <- which is like the found one (to get the relevance)
At the end I need to sort the found entries by their relevance to provide the 10 most relevant entries.
(The real query checks for more columns than just 2, but for dummy reasons are 2 okay)
The query which selects the rows where the value is like...
select * from
(
(select department from entries where department like '%myVariable%')
OR
(select grade from entries where grade like '%myVariable%')
)
I think you know what I mean. Does anyone have any hints, suggestions, examples or useful links for me?
Thanks in advance!
Best regards,
FriaN
Why not use union all here?
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%'
Then this should order the results for you:
select department, count(*) cnt from (
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%')a
group by department
order by count(*) desc

php select from 2 tables and order by

i have 2 tables:
1st table is called maxlift with 1 column called, exercise
2nd table is called workout with 5 columns called, exercise, sets, reps, weight, date
i want display the highest weight for each exercise where exercise in both tables is the same.
i am using:
`SELECT *
FROM workout, maxlift
WHERE workout.exercise = maxlift.exercise
GROUP BY maxlift.exercise
ORDER BY workout.weight desc`
the problem i have is the weight displayed is not the highest weight from table workout.
thanks for any help.
You can use the aggregate function max() for this.
SELECT workout.exercise, max(weight) as weight
FROM workout, maxlift
WHERE workout.exercise=maxlift.exercise
GROUP BY workout.exercise;
Now, you basically group by exercise and find the maximum weight for each such group using the max() function. You can alias your selected column using column_name as alias.
`SELECT *
FROM workout, maxlift
WHERE workout.exercise = maxlift.exercise
ORDER BY workout.weight desc`
use this by removing group by and let me know about it

How to get only 1 row of each similar content?

I Have a private message system on my site and i'm trying to pull all subject's on the messages.
I need to show any subject once.
so if I have this subjects:
hey
hello
hey
good morning
good morning
I need to print this:
hey
hello
good morning
I can just cross it out with if, but I guess there is a better way with sql.
Thank you.
Option 1 : DISTINCT
SELECT DISTINCT subject
FROM my_table
Option 2 : GROUP BY
SELECT subjects
FROM my_table
GROUP BY subjects
Difference between GROUP BY and DISTINCT
Distinct is used to filter unique records out of the records that satisfy the query criteria.
Group by clause is used to group the data upon which the aggregate functions are fired and the output is returned based on the columns in the group by clause. It has its own limitations such as all the columns that are in the select query apart from the aggregate functions have to be the part of the Group by clause.
See this and this for the reference .
Try something like:
SELECT DISTINCT subject FROM emails;
If you use SQL.
select distinct subjects
from YOUR_TABLE.
or
Select subjects
from YOU_TABLE
group by subjects

php mysql inner join - get a particular row

This is my query to get model information from one table and a single picture from another table. What changes do I have to make to this query in order for it to get the picture where ORDER BY sort DESC? In the table of the pictures, there is a field by the name "sort". The default value for the field for each row is 0. But one random row has the value of 1. I want to get that particular row. I don't, however, want to use WHERE sort=1 because then even in the case where no row has the sort value 1, one row should still get fetched.
$sql="SELECT tc.id,tc.alias,tc.firstname,tci.imagename
FROM ".$pre."models tc
INNER JOIN ".$pre."model_images tci ON tc.id=tci.userid
WHERE activated=1 AND sex=$sex AND city=$city AND published=1
GROUP BY tc.id ORDER BY firstname ASC";
Thank you in advance!
Solved using:
SELECT tc.id,tc.alias,tc.firstname,
(SELECT imagename FROM ".$pre."model_images WHERE userid= tc.id
ORDER BY sort DESC LIMIT 1) AS imagename
FROM ".$pre."models tc
WHERE tc.activated=1 AND tc.sex=1 AND tc.city=2 AND tc.published=1
ORDER BY tc.firstname ASC
You should place that in your WHERE clause aswell. One t hing to note though is to be carefull with the way you're using the column names. It's better to tell to which table they belong.
So this:
WHERE activated=1 AND sex=$sex AND city=$city AND published=1
Should be:
WHERE tc.activated=1 AND tc.sex=$sex AND tc.city=$city AND tc.published=1
And then simply add the 'sort' column to it:
WHERE tc.activated=1 AND tc.sex=$sex AND tc.city=$city AND tc.published=1 AND tci.sort=1
If no results are returned, then make sure that there are records that meet the required conditions. Because there's nothing wrong with the query. Try to print your query to the screen etc. to see if every variables has a value.
edit:
You should lose the GROUP BY.
SELECT tc.id,tc.alias,tc.firstname,tci.imagename
FROM ".$pre."models tc
INNER JOIN ".$pre."model_images tci ON tc.id=tci.userid
WHERE tc.activated=1 AND tc.sex=$sex AND tc.city=$city AND tc.published=1 AND tci.sort=1

Categories