Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I´m trying to get entries from my mysql database which represent a certain "hotness". Let´s say those entries are music tracks which carry attributes like "played" (how many times this track was played) and the attribute "added" (when the track was added (in timestamp format)). I already have the category "newest", which filters for the added-attribute and the category "top" which filters for the views-attribute. Now I need the category "hot" which should combine both. I came up with this formula: hotness = views / lifetime. So if a track has been played a lot of times but added recently it might be hot. And the other way round. Does that make sense? Anyway: How can I create a sql query in php which gives me the entries which have the highest "hotness"?
My query for the top-category:
$sql = "SELECT * FROM tracks ORDER BY views DESC LIMIT 0,35";
query for new-category:
$sql = "SELECT * FROM tracks ORDER BY added DESC LIMIT 0,35";
Thanks in advance.
To be really able to track the hotness, you have to have a history of when which track was played.
The total number of times a track was played and the date it was added added only allows for querying for all-time popular. The case that a track was added years ago but is getting popular now cannot be distinguished from it was added years ago, was popular back then but is not anymore.
You could add a new table where you store a date, a track ID and the number of times it was played on that date. Then you can query for all those tracks that were played a lot in the last day, 10 days and so on.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am building a mysql database to store golf scores.
I am not sure on the best approach to store the round information.
Each round is made up of either 9 or 18 holes and for each hole I need to store
Hole id
Number of shots
Stableford points
Green in regulation
Fairway hit
Number of putts
Number of penalty shots
My question is should I have one huge table, that stores all of this. Like a rounds table. and have the above 7 fields 18 times for each hole.
Or should I have a smaller rounds table that just contains the date played etc and then another table such as scores that just has the 7 fields, and have multiple rows in that table to make up the complete round?
I guess I am asking in terms of which would perform better and which is the better design?
Thanks
Definitely two tables. First, let's name it rounds will contain data relevant to round itself, such as date, id of the golf terrain etc. The other, let's name it hole, will have 7 aforementioned fields, together with round_id field that will reference round that particular hole belongs to.
Main benefits are clearer design and avoidance of redundant data. If you keep everything in one huge table, each row would need to contain not just fields relevant to the single hole, but also fields relevant to the whole round (date, id of the terrain..) -> same data in many rows, unnecessary. What if you mistakenly enter wrong date? You would have to change it in all 9 to 18 rows instead of only one.
See also:
Normalization in MySQL
Database normalization
Divide your information as much as possible. Otherwise you'll face alot of redundant data.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm creating a function that retrieves records based on several different fields and then stores those records so we can query them later.
When querying the records, we're selecting a record to be used elsewhere if it matches the requirements of another set of fields/criteria.
For example:
Returning employee records so that one of them can be selected for a new position (management maybe?) based on fields such as skill set, location, and so on...
The problem
Say I loop through these stored records (in an array or something similar) and I check if the first employee in the array is suitable for the position, then find out they are, so then add them to the position, I'm neglecting the other x amount of employees that have been stored. I feel like this would be an issue because it might turn out that the order in which someone is stored might determine the likelyhood they are chosen for the position.
I thought this might be solved by creating a sorting function to sort the stored records based on the employers preferences (location, salary, availability,...), although I'm not sure how to implement this.
I'm wondering if there is any built in MySQL functions that would help sort the records based on something?
This might be something I might have to figure out on my own, but I thought I'd ask just in case there was anything useful I could use.
I hope the question was clear. If not, please comment below.
Just thinking out loud - won't sorting create the same issue you are trying to avoid - unless you can come up with a weighted score...
create table as employees_to_consider as
select e.*,
availability_score*availablity_factor
+ salary_score*salary_factor
+ location_score*location_factor as weighted_score
from employees e
where -- whatever your criteria is for selection here
order by weighted_score
The real task is deciding how to determine the score for each factor and what the appropriate weight should be.
For example, salary score can be determined by taking the ratio of what the employee is willing to work for by the target. If they match, it can be scored as 50. The number can be increased by the amount the employee is willing to work under the target and decreased by an amount they are over. Salary is pretty important so the factor might be 33%.
Similarly, if the employee lives with 15 minutes, then they can be scored at 75, within 30 at 50, over 30 25. Location is not as important as salary so it is scored at 10%.
Hopefully, you will be able to assign meaningful scores and factors to each measure.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a custom-built CMS with php & mysql, and I was wondering which is the best way to put an old article (for example) posted in 2013 back to top (so I don't have to re-post it). It's like a "bump" (bring up my post) which is used in forums.
I was thiking to add a new date_order column to the Articles tables in mysql, and then "order by" that field, so the original posting date won't change, and anytime I want to bring up a post, I just "bump" it (change that fields date to the current timestamp). But this will increase the database size of course.
Any beter idea?
Add a tinyint column called "pinned" with a default value of 0. Make the value of the pinned field 1 for any post you want pinned to the top.
In your SQL string ORDER BY pinned DESC, DATE (or whatever).
Using this method, you could have a button that only you can see (because you are logged in), clicking it would set pinned = 1 for that post.
Without changing the table structure you could favor some ids in your ordering, e.g.
SELECT * FROM articles ORDER BY FIELD(id, 11, 51), date ASC
This would first display the articles with id 11 and 51, then the rest ordered by date.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
most likely a clueless question but I would like to start off on the good foot:
Despite trying my best, I have actually never really learned to program and I'm kind of "learning as I go" so please excuse me if this seems very obvious to you...
It's more of a suggestion and feedback kind of question rather than pure programming.
My situation is the following:
I'm building a racing game that would receive various inputs from a number of users (through a php website), I am storing that information in a MySQL database, and once a week I would like to process all that information to generate "lap times", which will then create a race (my "output").
Not taking into account the various methods of calculating that output, I need to do two important things which I'm not sure how to begin with at all :
1) Storing the race information for every user (lap time per lap, fastest lap, race position per lap, race position at end of race, award points depending on the position).
Where and how should I optimally store those informations ?
I have created a race DB with a unique identifier that auto increments, I'm thinking I will generate 1 set of data for each race, so should I store all the information pertaining to that race in there ?
Would I then create a data row (with type time?) for the lap time informations (1 row for lap1, 1 row for fastest, etc... ?)? But how would I know which user (I have a unique userID for each) did which lap (how would I assign the userID to the lap time)?
2) At the end of the race I need to award points depending on race position at the end, should I just compare total lap times (additional row?) and sort by lowest first ? The points data would be stored in the user DB ?
I appreciate any input you might have for the modeling of this project !
Drop every lap_round, lap_time and position in the DB and add a user_id and a race_id.
Afterwards query the laps. That way you can tell which is fastest overall, fastest per user, time per lap and much more.
To get the position query the db for the last lap. It holds its position.
Points are user based, so put them in the user table. Just add. But if you want to tell how many points were added per race than make a seperate table points (user_id, race_id, points)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got a database with a lot of articles created through time. Now I want my script to modify a "popularity" field in the database based on hits, likes, dislikes and time.
How would you do that? The older the article the less relevant of course. But if the article is two weeks old BUT got a lot of hits and likes I want it to show up even though.
Any ideas?
Considering you already have all the data, I would calculate the popularity like follows (in pseudo code):
Popularity = (1 / now - time) + likes - dislikes.
So in MySQL query it would be something like this:
UPDATE articles SET
popularity = (now() - article.timestamp) + article.likes - article.dislikes;
After the query is run and the data updated, you could apply the sorting on your data to fetch the most popular artcles:
SELECT * FROM articles
ORDER BY popularity DESC.
Hope this helps.