Timestamp comparison on where clause randomly working?

I have an old database where the column FastStart_start and FastStart_End are varchars.

Using the Laravel query builder, the following query

$referrals = DB::table('users')->join('order_details', 'order_details.user_id', 'users.id')
->join('users as sponsor', 'sponsor.id', '=', 'users.sponsor_id')
//->where('order_details.created_at', '>=', 'sponsor.FastStart_Start')
->where('order_details.created_at', '<=', 'sponsor.FastStart_End')
->select('users.username', 'users.email', 'order_details.created_at', 'users.sponsor_id',
'sponsor.FastStart_Start', 'sponsor.FastStart_End')
->get();

gives this outcome:

 #items: array:3 [
0 => {#2792
+"username": "annalise92"
+"email": "[email protected]"
+"created_at": "2022-06-15 21:32:23"
+"sponsor_id": 2085929
+"FastStart_Start": "2022-06-01 00:00:00"
+"FastStart_End": "2022-06-30 00:00:00"
}
1 => {#2832
+"username": "nbarton"
+"email": "[email protected]"
+"created_at": "2022-06-15 21:32:23"
+"sponsor_id": 2085929
+"FastStart_Start": "2022-06-01 00:00:00"
+"FastStart_End": "2022-06-30 00:00:00"
}
2 => {#2836
+"username": "maxine56"
+"email": "[email protected]"
+"created_at": "2022-06-15 21:32:23"
+"sponsor_id": 2085929
+"FastStart_Start": "2022-06-01 00:00:00"
+"FastStart_End": "2022-06-30 00:00:00"
}
]

From the outcome its clear that FastStart_Start is less then order_details.created_at.

However, if I uncomment the line

//->where('order_details.created_at', '>=', 'sponsor.FastStart_Start')

from the query builder, the result set will be empty.

How is this possible? Does it have to do with the fact that FastStart_Start is a varchar column? If so, why does it not affect the comparison with FastStart_End?

Using toSql command I see that query builder is creating this query:

select users.username
, users.email
, order_details.created_at
, users.sponsor_id
, sponsor.FastStart_Start
, sponsor.FastStart_End
from users
join order_details
on order_details.user_id = users.id
join users sponsor
on sponsor.id = users.sponsor_id
where order_details.created_at >= ?
and order_details.created_at <= ?

Any idea why the resulting set is empty if I uncomment the where line?

EDIT: The tosql result for the other query is:

select users.username
, users.email
, order_details.created_at
, users.sponsor_id
, sponsor.FastStart_Start
, sponsor.FastStart_End
from users
join order_details
on order_details.user_id = users.id
join users sponsor
on sponsor.id = users.sponsor_id
where order_details.created_at <= ?

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/2HFaiVW
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.