I have a column with datetime data type and I want to build a SQL query in Zend Framework2 which compare date part with user input date.
Need to build similar part as DATE(datetime column) = '2014-09-16' with;
$select->where();
would be very grateful if someone could help on this.
Use like this:
$date = '2014.05.24';
$select->where('date(expecting_date) = "'.$date.'"');
You should use predicate expression for these kind of conditions, like :
$select = new \Zend\Db\Sql\Select(table name);
$select->where(new \Zend\Db\Sql\Predicate\Expression('DATE(datetime) = ?', '2014-09-16'));
Related
In users table I have a column call "month".
I want to list all users that meet the condition: current month - user month <=2
Here my code
$time = new \DateTime('now');
$today = $time->format('m');
$users = Users::find()->where(['<=', 'month' - $today, 2])->all();
But this code is wrong. Please help me with this.
Hope this all make sense.
Thank you!
In Yii2 you can use different format for buil where condition
for this kind of situatio is useful use string format with param
in string format you can pass the literal string and param this way
$users = Users::find()->where('(month - :today ) <= 2' , [':today'=>$today])->all();
see this for more http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
I'd like to make a query on dates using SequelizeJS but i don't know how to do and there is nothing on that on the website...
My code :
var day = request.params.day;
Appointment.findAll({where: ["start.day() = day"]}); // start is my column, format with DATETIME
Depending on your DB there might be some function to extract the day from the column. Never seen the .day syntax before though.
Appointment.findAll({
where: sequelize.where(sequelize.fn('day', sequelize.col('start')), day)
});
On latest master this should produce something like
WHERE DAY("start") = day
You can just use the regular comparison operator, as long as your date is in a valid format. For this purpose you can just create a Date object out of the input, and then pass it to the Sequelize query, like this:
var day = new Date(request.params.day);
Appointment.findAll({where: ['start > ?', day]}).then(function (result) {
// do stuff here
});
My database (mysql) tables use TIMESTAMP columns, and whenever I want them returned in a query, I want them to be queried as "UNIX_TIMESTAMP(columnname)".
How do you easily modify queries in zend framework to achieve this?
For example, the current code is:
select = $this->select();
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
This eventually becomes:
select * from tablename where user_id = 42;
I want something that automatically finds the TIMESTAMP column and changes the resulting query to:
select user_id,name,unix_timestamp(created) where user_id = 42;
I know I can use a MySQL view to achieve this, but I'd rather avoid that.
Thanks.
RR
You should be able to specify the fields you want in the select using the $select->from() object.
Zend_Db_Select
You should end up with something like this.
$select = $this->select();
$select->from(
array('t' => 'tablename'),
array('user_id', 'name', 'UNIX_TIMESTAMP(created)')
);
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
If you wanted to run an expression that doesn't have parenthese in the function, Use the Zend_Db_Expr() method to escape the query properly.
I'm trying to match md5(ID) to an id.
SELECT *
FROM `user` u
WHERE
MD5(`user_id`) = '66f041e16a60928b05a7e228a89c3799'
this is ID = 58
I tried something like this. I know I'm close I just don't know what I'm missing
$criteria = new Criteria();
$criteria->addAnd('md5('.User::USER_ID.')', $_REQUEST['fs'], Criteria::CUSTOM);
$user = UserPeer::doSelectOne($criteria);
Any ideas?
First of all, directly using Criteria objects is deprecated not recommended. You should use Active Query classes.
Using these classes, you will be able to write stuff like this :
UserQuery::create()
->where('md5(User.Password) = ?', $_REQUEST['fs'], PDO::PARAM_STR)
->findOne();
You'll notice that I use the PhpName both of the table and the column in the query.
EDIT : For raw conditions, the parameter type has to be specified. You'll find more information on this issue.
After lenghty T&E process I managed to get it done like this
$c = new Criteria();
$c->add(UserPeer::USER_ID, "md5(user.user_id) = \"".$_REQUEST['fs']."\"", Criteria::CUSTOM); // risk of SQL injection!!
$saved_search = UserPeer::doSelectOne($c);
For some reason PropelORM though that $_REQUEST['fs'] was name of the table rather than the value. \"" solved the problem.
I am tring to compare a date from a asp calendar control to a date in the table.... here's what i have... it doesn't like the == ?
var query = from details in db.TD_TravelCalendar_Details
where details.StartDate == calStartDate.SelectedDate
&& details.EndDate == calEndDate.SelectedDate
select details;
In order for your query to work both details.StartDate and calStartDate.SelectedDate must be typed as System.DateTime.
What error are you receiving? Most likely one of these properties is a string and will need to be parsed into a DateTime instance for comparison.
What is the Type of details.StartDate and details.EndDate? Is it String? Maybe that's the problem. If the database type is String you should Parse the Date String into a DateTime and compare it with the calendar's selected date then.