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
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 5 years ago.
Improve this question
please how can i query the database table one row at a time without repeating the same row until all the rows has been selected.
E.g
I want the query to select the first row the first time and second row the second until all the rows is selected. Note i don't want to use random selection has result may repeat itself.
Thank you in advance
mysql has an OFFSET option that will work perfectly for what you want to (not that i understand why you are doing what you want to do) do. eg
$offset = 0;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;
$offset++;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;//offset is now 1
$offset++;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;//offset is now 2
I think in your case you would chuck it in a loop. I'm just showing it as above to illustrate the concept. Or maybe you will pass $offset as a parameter. Either way this should give you a good idea
What does offset do? It basically ignores the records before the offset count. so if i have records 1,2,3,4,5 and I select from this list with an offset of 2 I will get 3,4,5 as my list.
W3 Will explain it better than me
So this question is regarding an API request and response
Your response to the first api request should contain last_offset. Tell your consumer to pass offset as a parameter on their request. my.api.com?offset=1 after validation and sanitation of the parameter offset, you can include the value in your query else use default offset of 0
If they received last_offset=2 in the last response they should be passing offset=3 on the next request.
One way is make the primary key of the table a sequence of numbers starting from an arbitrary number.
Get hold of the starting index.
While writing the sql query,just write a loop and make the where=id + 1.This will give you each row at a time.
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 6 years ago.
Improve this question
I make some website which will be similar to basic TODO List applications. For a single row I always store single date and I basicly dont need hours and seconds, date is just fine.
How I use data:
I have PHP backend which make SQL select and then create JSON from this data and give it frontend which is created in AngularJS.
What I want to do with data:
I want to compare data and visualize it with graphs. I am not sure whats the best way to store this kind of values.
Things I consider:
Unix TIME - I feel like its really easy to work with numbers in any language and also easy to compare and visualize
MySQL Date - I'm not sure because I think that inside MySQL date format should be really usefull but I'm not sure how to compare date in format like: YYYY-MM-DD
MySQL Datetime - Basicly same as before just with added time, I dont think this can be usefull for me.
I will be really happy your opinion and advise which can help me. Also advantage of each method because I mentioned just advantage of UNIX TIME because number are easy to compare. It's also easy I miss something relevant how to store date in database so if there is other option please don't hesitate to speak about it.
Thanks
I would opt for MySQL Date.
Lets say we have a table called my_table which has a Date column named my_date.
You can actually interact with it in much the same way as you can a timestamp. Assuming my_date is the date that the item was created...
// Where it was created after 1st Jan
SELECT * FROM my_table WHERE my_date > '2016-01-01'
// Where it was created in Jan
SELECT * FROM my_table WHERE my_date BETWEEN '2016-01-01' AND 2015-01-31
// Where it was created in the last 7 days
SELECT * FROM my_table WHERE DATEDIFF(CURDATE(), my_date) <= 7
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to get order numbers by ASC.
In my mysql table It written with - (ex. 1-2; 2-3; 15-60 etc.)
Now I get order:
15-60; 1-2; 2-3;
why not 1-2; 2-3; 15-60?
I'm making new sorting in my opencart.
Tutorial to make new sorting I used: sort by size opencart
You can try with:
SELECT *
FROM yourtable
ORDER BY numbers + 0
to automatically cast you VARCHAR column to a number, and apply the order correctly.
Please see fiddle here.
Assuming that your order numbers are in fact strings of digits interspersed with hyphens, you are getting the string sort order by default.
You can cast strings to integers, but perhaps a better approach is to store order numbers appropriately padded by zero digit characters, so that 15-60, 1-2, 2-3 are stored as 015-060, 001-002, 002-003 (or whatever degree of padding is appropriate). Then the string sort order and numeric sort order will be the same, and you don't need to do casting.
you can also use this
SELECT *
FROM yourtable
ORDER BY CAST(numbers as unsigned)
DEMO HERE
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
I want to obtain the last value that I put in.
I am trying to obtain the number on which my article is. So I use this query:
$totalPages= mysql_query("SELECT page FROM `easy_db`.`article` ORDER BY article_id DESC LIMIT 1") or die(mysql_error());
The value that I get is 6.
Why 6? I truncated the table..and tried again and I get 6 again.. It should be 1.
Another question, I want to enforce types in php..
I try to enforce an Int, but it doesnt seem to work:
$page=(int) $totalPages/10;
mysql_query returns a resource handle, not the result of the query itself. See the PHP manual for some basic examples of how to query a database. For example:
$result= mysql_query("SELECT page FROM `easy_db`.`article` ORDER BY article_id DESC LIMIT 1") or die(mysql_error());
if ($row = mysql_fetch_assoc($result)) {
$totalPages=$row['page'];
}
With regard to your second question, pay attention to the operator precedence rules - the (int) operator has a higher precedence than division, so is carried out before the division. Thus, you can get a floating point result. Alternatively try one of these
$page = (int)($totalPages/10); #rounds down
$page=round($totalPages/10); #rounds up or down as appropriate
Also checkout floor() and ceil() as alternatives to round, as they are often useful when writing pagination calculations.
You can use mysql_insert_id() function in php to retrieve the id of the last inserted row.
mysql_insert_id
You should use mysql_insert_id(); immediately after your insert statement.