Maybe a stupid question, but I can't find anything on it. I know you can do counts and some basic math with queries.
Is there a way to make the database count and return results with the highest or lowest number of occurrences?
Lets say you had all the pages of a library in a database and you wanted to know the top 5 characters used or maybe the least used.
I guess the easiest example would be the lottery. Lets say you had a table of past lottery results. Lets say you wanted to return a list of the top 10 most drawn numbers. Lets say the numbers can range from 1 to 100.
It wouldn't be very efficient to run the following query 100 different times and then run some php to sort the data.
SELECT COUNT(*) FROM mytable WHERE ball=1
Surely there must be a way to return a count of the most duplicated data.
Perhaps I am over thinking this. Anyway, thanks for any help in the matter.
That's called grouping, the group by clause will let you do that, make sure you don't just pull out the count but also the thing you're counting, in this case ball.
Get the lowest number occurrences:
SELECT ball, COUNT(*) FROM mytable GROUP BY ball ORDER BY ball ASC limit 1;
Get the highest number occurrences:
SELECT ball, COUNT(*) FROM mytable GROUP BY ball ORDER BY ball DESC limit 1;
If you want them all in one result you can use a union/union all:
(SELECT ball, COUNT(*) FROM mytable GROUP BY ball ORDER BY ball ASC limit 1)
UNION ALL
(SELECT ball, COUNT(*) FROM mytable GROUP BY ball ORDER BY ball DESC limit 1);
you can simply use
SELECT column, COUNT(*)
FROM mytable
GROUP BY column_name
this will count all of the existing entries for the specific column_name
you should use column before COUNT(*) so you know what has that many values.
i hope this helps!
Related
So hoping someone can help me on this. I've read so many questions on the same issue but none answer my question. I have the following SQL SELECT statement in my PHP code:
$sql = "
SELECT DISTINCT
pics.piclocation,
pics.picid,
rating
FROM
pics,
ratings
WHERE
pics.milfid = ratings.picid
ORDER BY
ratings.rating DESC
";
However as many know the DISTINCT keyword does not eliminate duplicate rows all the time. The problem is that the same picture is getting output often more than once i.e. pics.piclocation. The idea is each pic is rated from 1 to 5 which is then inserted into the ratings table.
Any ideas on how I can produce an output and eliminate duplication piclocation rows? I want the pictures to be listed based on which picture has the most 5 ratings if that helps any.
Thanks!
So if you only want images that have a rating of 5 then go ahead and select on that, then you can count the number of 5's that image has gotten and order by that. You will also then need to group by the unique identifer for an image which looks like its pics.milfid and ratings.picid.
SELECT
pics.piclocation,
pics.picid,
COUNT(ratings.rating) as nb_ratings
FROM
pics,
ratings
WHERE
pics.milfid = ratings.picid
AND ratings.rating = 5
GROUP BY
ratings.picid
ORDER BY
nb_ratings DESC
If instead you just wanted the highest rated pic, then you could SUM the ratings:
SELECT
pics.piclocation,
pics.picid,
SUM(rating) as total_rating
FROM
pics,
ratings
WHERE
pics.milfid = ratings.picid
GROUP BY
ratings.picid
ORDER BY
total_rating DESC
I'm working on php small project, here I need first 5 records from beginning of records and last record 1 from end of the table's record. I don't know how to write a single mysqli query.
Any help will be appreciated. Thanks in advance.
SQL tables represent unordered sets. So, there is no such thing as the first five rows or last row -- unless a column explicitly defines the ordering.
Often, a table has some sort of auto-incremented id column, which can be used for this purpose. If so, you can do:
(select t.*
from t
order by id asc
limit 5
) union all
(select t.*
from t
order by id desc
limit 1
);
Notes:
Sometimes, an insert date/time column is the appropriate column to use.
You want to use union all rather than union -- unless you want to incur the overhead of removing duplicate values.
For this formulation, if there are fewer than 6 rows, then you will get a duplicate.
The UNION operator allows this, carefully toying with the ORDER BY and LIMIT clauses :
(SELECT * FROM table ORDER BY field ASC LIMIT 5)
UNION
(SELECT * FROM table ORDER BY field DESC LIMIT 1)
I have a database table questions on a online web server with 2000+ rows and I need to get 6 randomly selected rows. They must be different so that one question is not two times in the list array of 6 questions.
How can I achieve this?
You have a relatively small amount of data, so the simplest method is:
select q.*
from questions q
order by rand()
limit 6;
In this query, the order by takes the longest amount of time. Ordering 2,000 rows might be noticeable. A simple fix is to reduce the number of rows being ordered. One method is:
select q.*
from questions q cross join
(select count(*) as cnt from questions) m
where rand() < 100 / m.cnt
order by rand()
limit 6;
The where selects about 100 rows randomly and then orders those to select 6. You are pretty much guaranteed that the where will always choose at least 6 rows.
Use the DISTINCT operator in MySQL:
SELECT DISTINCT column FROM table ORDER BY RAND() LIMIT 6;
So DISTINCT will take care and remove duplicates
I'm currently looking in MySQL to order results by price, and then output a random one with the highest price. (several will have the same price)
I've had a look at other stackoverflow questions and found people with answers saying that if two results are the same you cannot guarantee which one will be outputted however that sounds like a bit of a glitch/hack, is there anyway to order a table by the highest results and then chose a random one of those results?
$qry="SELECT * FROM campaignInformation ORDER BY campaignInformation.bidPerCustomer DESC LIMIT 1";
two of my "bidPerCustomers" are 0.25 and I would like to to chose a random one of these every time, however not choose one with a bet of 0.11 for example
Thanks in advance guys!
I'm asumming that I will have to make a query and then choose a random one from the results however it would be nice if I could do it in the query to begin with.
If you just want to select any from those with max value, then you can build it up cleanly:
SELECT * FROM campaignInformation C WHERE C.bidPerCustomer = (
SELECT MAX(D.bidPerCustomer) FROM campaignInformation D
)
That'll net you all rows that have max value. You can then select one from that table, LIMIT 1, order by RAND()
SELECT * FROM (
SELECT * FROM campaignInformation C WHERE C.bidPerCustomer = (
SELECT MAX(D.bidPerCustomer) FROM campaignInformation D
)
) AS X
ORDER BY RAND()
LIMIT 1
Every derived table needs an alias, hence the 'AS X'
Make sure you have an index on your bidPerCustomer column, or havoc ensues.
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