Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Ok, I've got a quick question. I'm using PDO to communicated with a MySQL db. Using BETWEEN with two dates as the variable is easy:
$db->query("SELECT * FROM awesome WHERE date BETWEEN :start AND :end");
What I don't think is easy is when I have one date and two columns. This doesn't work, no matter how loudly I cuss:
$db->query("SELECT * FROM awesome WHERE :one_date BETWEEN start_col AND end_col");
Is there a way to use BETWEEN without reverting to something awful like...?
$db->query("SELECT * FROM awesome WHERE '$one_date' BETWEEN start_col AND end_col");
Or should I just stick to not using BETWEEN in this case?
$db->query("SELECT * FROM awesome WHERE start_col<=:one_date1 AND end_col>=:one_date2")
Thanks!
BETWEEN only works with one column and two values.
But your last approach should work. That's at least the way to go
$db->query("SELECT * FROM awesome" .
" WHERE start_col <= :one_date1 AND end_col >= :one_date2"
);
When using named parameters you should also be able to use the same name twice and only need to bind once.
Update
Well, just tested it and worked for me with " WHERE 'value' between col1 and col2". (tested postgres with datetime, mysql with integer)
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 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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to get data from database from specific row to last row.
How can I do it?
You can set limit and offset with your sql query like this.
SELECT *
FROM your_table
ORDER BY id DESC
LIMIT 1, 1;
See more : http://www.mysqltutorial.org/mysql-limit.aspx
$sql="Select * from table order by id desc limit 20";//It will give you last 20 records
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This query runs well in mysql but cannot run in mssql. Why?
Don't judge me, I am new in mssql.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
ORDER BY columnindex DESC
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'order'.
You just need to remove ORDER clause. ORDER is pointless in an UPDATE statement.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
You don't need at all the ORDER BY clause. this is only when SELECTing data from the database.
You may want to note that even though SQL is a language with a specified structure, different SQL engines may parse the query differently and use different syntax since a lot of the SQL functionality is not defined by the language.
for example in mysql you may use the LIMIT keyword for limiting the number of records that are returned and in mssql you may use ROWNUMBER and oracle uses the ROWNUM system.
UPDATE cvcolumnlist
SET columnindex = columnindex+1
WHERE cvid = 40
AND columnindex > 2
this should work
UPDATE [cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3
I'm not sure if this applies. Most of my queries use something like this.
UPDATE [database].[dbo].[cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3
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.