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.
Related
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.
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 using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get best time from MySql table collumn which I made varchar (I can change it, just I didn't know better) which data looks like "00:05:22.22" (it's "hours:minutes:seconds.milliseconds").
it's like laps times in that collumn..
or I should change my DB table structure? to what?
and how I should do it then?
i.e.
in that collumn are records like this:
00:00:04.99
00:00:04.57
00:00:04.55
00:00:04.58
00:00:03.36
And I would like to get all of them ordered by shortest time. Like best lap.
If you are using MySQL 5.6.4 or greater, you can use the TIME datatype for your column and indicate that it should accept fractional parts of a second - see here.
Otherwise, using a VARCHAR column, you can still use the MIN/MAX functions for your data and expect to get a valid result (provided all values are correctly formatted), since alphabetically sorting the data in your use case should give the same result as numerically sorting it.
Alternatively, you can use the ORDER BY clause on that column and take just the first n results. Depends on your needs.
To get an ordered set from a table your query needs to specify an order -
SELECT *
FROM `table`
ORDER BY `lap_time` ASC
You do not have to include ASC in the query as any ORDER BY will sort from smallest to largest unless otherwise told not to do so with DESC
As you have said, your column datatype is varchar, you can do
select [column_names] from [table_name] order by [column_name_containing_time] asc
You can see an example here: SQL Fiddle
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my question in Unexpected entry in a <select> field! How did this happen? , I had asked about an unexpected 01,05 entry in a field that stored values from a <FORM><SELECT>. The input did not have MULTIPLE enabled and should have had the default of single select.
Yet it allowed a user to make multiple selections. I have now realized that in my current scenario, there are several users who would need to select more than one option. So now I am faced with another question.
What if, I were to need to query the database for values in that same field? Would multiple entries stored in the field screw up the results?
Let us say that the <SELECT> asked the user to select between 01 and 04. My expectation would be that the field will store either 01, or 02, or 03, or 04 and I might query the table to return all rows that contained '02'.
What if one of the users has made multiple selections - say 02 and 04. In the database there would now be an entry like 02,04 in that field. If I were to query for 02 (or 04 for that matter), would I get the 02,04 entry in the result along with the ones that contained only '02' in that column?
What are the conditions under which I could face a select query that did not return all the rows I need?
You can use the MySQL IN Clause to check whether a value is found inside a list of values.
A better approach perhaps, is to employ a single-to-many relationship table. (Look it up)