Select most frequent from column in - mysql

Hi i have a columm in the table and i want to select the most common item from the selected column. The table is set up
publication:
id
Title
published
I want to beable to select the most recurring places where publications have been published. Is this possible to do?
Thanks in Advance
Dean

select published, count(*) nbr
from table1
group by published
order by nbr desc
limit 1
You don't really need the count, but if you wanted confirmation that the choice seemed reasonable, you could use it. Also, you didn't specifically say whether you wanted ONLY the one, or wanted to see which was the most frequent, along with frequencies of the other records. Take off the limit 1 if you want to see all records.

Related

Limiting the count query in MySQL?

I am trying to do a simple test where I'm pulling from a table the information of a specific part number as such:
SELECT *
FROM table_name
WHERE part_no IN ('abc123')
This returns 25 rows. Now I want to count the number that meet the "accepted" condition in a specific column but the result is limited to only the 10 most recent. My approach is to write it as follows:
Select Count(*)
FROM table_name
WHERE part_no IN ('abc123') AND lot IN ('accepted')
ORDER BY date DESC
LIMIT 10
I'm having a hard time to get the ORDER BY and LIMIT operations to work. I could use help just getting it to limit appropriately, and I can figure out the rest from there.
Edit: I understand that the operations are happening on the COUNT which only returns one row with a value; but I put the second clip to show where I am stuck in my thought process.
Your query SELECT Count(*) FROM ... will always return exactly one row.
It's not 100% clear what exactly you want to do, but if you want to know how many of the last 10 have been accepted, you could use a subquery - something like:
SELECT COUNT(*) FROM (
SELECT lot
FROM table_name
WHERE part_no IN ('abc123')
ORDER BY date DESC
LIMIT 10
)
WHERE lot IN ('accepted')
The inner query will return the 10 most recent rows for part abc123, then the outer query will count the accepted ones.
There are also other solution (for example, you could have the inner query output a field that is 0 when the part is not accepted and 1 when the part is accepted, then take the sum). Depending on which exact dialect/database you are using, you may also have more elegant options.
Select count returns ONE ROW therefore the ORDER BY and the LIMIT will not work on the results

MySQL: Always select row with first of its kind

I have a table with multiple columns ordered by one attribute (Attr1) and I would like to select (with mySQL) always the first row of a new value:
How do I do this?
Thanks a lot!
EDIT:
Sorry, for not stating my question clear enough.
Consider this new example table:
Let's say I run a blog with several posts which can can be commented by users. On the first page (the starting page) I want to list the newest comment for each post. I therefore get all comments and order them first by post_id, then by time stamp (ORDER BY post_id, time stamp DESC).
However, as I said, I'm only interested in the newest comment (for each post), not all of them. The desired output is therefore the orange rows as it is the newest comment for post_id 1, post_id 2 and post_id 3.
If I solved this with PHP I would simply take the whole table and loop through all rows and only echo the comment if the post_id doesn't match with the previous one.
Hope that makes it clearer. Is this possible with MySQL?
Use Limit and order by clause
select * from your_table ORDER BY Attr1 desc Limit 1
If you want always the lowest ID as the first result AND you want all those lines which are orange, this would work for you!
SELECT * FROM your_table GROUPY BY Attr1 ORDER BY id
But as another answer said, he doesnt think that you want all those lines but instead only one then use his answer..
Database table:
Result:

Order by most searched words in MYSQL

I've designed a simple website which enables users to search on a MYSQL database. The search method is also simple: the user types a word in a textfield and it searches in the database.
Now i want to include in this website a table with the most searched words and i didn't find anything until now but this sentence:
select distinct column
, count(1) as total
from dept
group by column
order by total desc
limit 5
but this doesnt retrieve what i want. Do you have any idea of how I get this result?
Thank you in advance!
A simple example for a small site:
After each search, add a row to a table searched. Bonus points for adding a timestamp.
insert into searched (keyword, timestamp) values ('foo', 1234567890);
From there:
select keyword, count(*) as total from searched
group by keyword order by total desc limit 5;
Of course, for simple things like this, I'd use redis.

How to grab most popular rows in table?

I have a table with comments almost 2 million rows. We receive roughly 500 new comments per day. Each comment is assigned to a specific ID. I want to grab the most popular "discussions" based on the specific ID.
I have an index on the ID column.
What is best practice? Do I just group by this ID and then sort by the ID who has the most comments? Is this most efficient for a table this size?
Do I just group by this ID and then sort by the ID who has the most comments?
That's pretty much simply how I would do it. Let's just assume you want to retrieve the top 50:
SELECT id
FROM comments
GROUP BY id
ORDER BY COUNT(1) DESC
LIMIT 50
If your users are executing this query quite frequently in your application and you're finding that it's not running quite as fast as you'd like, one way you could optimize it is to store the result of the above query in a separate table (topdiscussions), and perhaps have a script or cron that runs intermittently every five minutes or so which would update that table.
Then in your application, just have your users select from the topdiscussions table so that they only need to select from 50 rows rather than 2 million.
The downside of this of course being that the selection will no longer be in real-time, but rather out of sync by up to five minutes or however often you want to update the table. How real-time you actually need it to be depends on the requirements of your system.
Edit: As per your comments to this answer, I know a little more about your schema and requirements. The following query retrieves the discussions that are the most active within the past day:
SELECT a.id, etc...
FROM discussions a
INNER JOIN comments b ON
a.id = b.discussion_id AND
b.date_posted > NOW() - INTERVAL 1 DAY
GROUP BY a.id
ORDER BY COUNT(1) DESC
LIMIT 50
I don't know your field names, but that's the general idea.
If I understand your question, the ID indicates the discussion to which a comment is attached. So, first you would need some notion of most popular.
1) Initialize a "Comment total" table by counting up comments by ID and setting a column called 'delta' to 0.
2) Periodically
2.1) Count the comments by ID
2.2) Subtract the old count from the new count and store the value into the delta column.
2.3) Replace the count of comments with the new count.
3) Select the 10 'hottest' discussions by selecting 10 row from comment total in order of descending delta.
Now the rest is trivial. That's just the comments whose discussion ID matches the ones you found in step 3.

MySQL: Best way to combine multiple MAX() selects with an array?

I hope someone could give me a general direction on this problem:
The starting point is an array of ids of db records.
array ids = [45,23,14,7];
Those records have some columns, i.e.
id,price,rating
7,$5.00,5
14,$2.00,4
23,$5.00,2
45,$5.00,5
What I would need is
the items with max(price) (or something equivalent).
if there is more than one item with the same price, get the ones with max(rating) (or something equivalent).
Finally, if there is still more than one item, take the one that comes first in the array.
I'm particularly stuck with point 3. Is there a way to do that in (My)SQL, or should I do that in code?
Thank you for your reading.
Something like this should work:
SELECT * FROM table WHERE id IN (45,23,14,7) ORDER BY price DESC, rating DESC LIMIT 1
In addition to the answer by #jasonlfunk you can add an extra order clause to take into account your array as well:
SELECT * FROM table WHERE id IN (45,23,14,7) ORDER BY price DESC, rating DESC, FIELD(id,45,23,14,7) ASC LIMIT 1
...I think about your point 3 ..it must be done in code, the result of mysql not necessarily returns results in the order or the array, if applying order by price, then by rating still returning more than one item your code should be able to receive a list instead a single row, and then make the comparisson in code.