Execute SQL query
Execute SQL query
wasimwani
Posts: 19Questions: 5Answers: 0
I want to retrieve only records belonging to current month from my table using sql query like
SELECT * from JKBINSR where MONTH(doi) = MONTH(CURRENT_DATE)
Just for experimental purposes i had made an attempt like this but it doesnt seem to work
->where( $key = 'MONTH(doi)', $value = 'MONTH(CURRENT_DATE)', $op = '=' )
How can one execute such query?
This discussion has been closed.
Replies
As I mentioned in the PM conversation you can use the sql() method if you are using the PHP libraries to execute custom SQL: docs.
Complex queries such as that can't be used with Editor though, since it doesn't "understand" SQL functions.
Allan
@allan any code snippet or example to use sql() method
From the documentation I linked to:
Expanded using
fetchAll()
:Allan
here is what i have tried so far but without success (What actually i was trying to do is to to retrieve only those records whose doi column value is equal to current month e.g only those records should be displayed whose doi value is Feb)
@allan Am i wrong here
Hi,
I've just been looking into this. The way to restrict the results to just records which match the current month is to use:
That uses PHP's
date()
of course. If you want to useCURRENT_DATE
it is slightly more complicated as you need to use a closure to access theQuery
class ownwhere()
method (rather thanEditor->where()
). The reason for this is that it provides the option to not bind the value (which would effectively escape it):Regards,
Allan
@allan : Thanks a lot allan ....that really does my job. Just one little question. What instead of current date we need set of records between range of dates. e.g from 1-feb-2015 till 28-feb-2015. what would the above query be like? I had tried like this
and like this
I'm not sure how it handles multiple where statements, but I'm pretty sure you don't want to use that operator for a range of dates. <> is the same as !=, so you'd literally need to put in every date that exists in your database minus the ones you want in your results for that to work.
What @allan showed you will give you the same results as a date range for an entire month, but I think a date range would look something like this:
Also based on allan's example, it looks like the first argument is the column, not the select. So you would want the SUM(amount) in your select.
@ignignokt The above query gives following error:
DataTables warning: table id=reminder - SQLSTATE[42S22]: Column not found: 1054 Unknown column '2015-02-01' in 'where clause'
@ignignokt The above query gives following error:
DataTables warning: table id=reminder - SQLSTATE[42S22]: Column not found: 1054 Unknown column '2015-02-01' in 'where clause'
Correct, because you are passing in the forth parameter as
false
which tells it not to bind the value, but rather to execute as SQL. Remove the, false
and @ignignokt's suggestion should work nicely.Allan
@allan That indeed did work :) You again saved my day.
.