I have to get rows within a range of passed symbol no.
Image of Table from where query is to be done
Query:
Mark::select('users_id', 'symbol_no', 'mark_obtained')
->where('subject_trade_id', 2)
->whereBetween('symbol_no', [100, 1000])
->orderBy('symbol_no')
->get();
This query returns no data, but I was expecting total 9 rows form the query.
If I dump the query, I find the query as expected.
Query Log Image
If I run the generated query to mysql then it woks fine.
Again, if I change the symbol no. range to something like this:
Mark::select('users_id', 'symbol_no', 'mark_obtained')
->where('subject_trade_id', 2)
->whereBetween('symbol_no', [10, 1011])
->orderBy('symbol_no')
->get();
It returns 2 rows this time, and this output is also wrong.
If I try changing symbol no. range and query like this:
Mark::select('users_id', 'symbol_no', 'mark_obtained')
->where('subject_trade_id', 2)
->whereBetween('symbol_no', [101, 200])
->orderBy('symbol_no')
->get();
Now it works fine as expected.
Found the problem
By mistake the symbol_no column was defined as varchar() which had to be int() so, whereBetween() was being unable to return expected data.
I faced same problem.. Could you please check "symbol_no" field type in database table, It should be number/integer.
Related
I'm trying to perform a query in Laravel 7 that gets the month part of a date field. I tried the following queries
$test = MyModal::selectRaw('month("date_col") as temp')->get();
$test = MyModal::select(DB::Raw('month("date_col") as temp'))->get();
$test = DB::table('my_table')->select('month("date_col") as temp')->get();
All variation of the same query. If I dump the query log, the query is correct for all 3
select month("date_col") as temp from "my_table"
If I run this on my DB I get the result as well. But I keep getting this error in laravel:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such function: month (SQL: select month("date_col") as temp from "my_table")
Am I doing something wrong? And is there a way to do this query in laravel?
Edit: Sorry forgot to mention this is with Laravel's unit testing
I think the problem in the way you use Month function
you don't set quotations for the column name, it should be:
$test = MyModal::selectRaw('month(date_col) as temp')->get();
it should work
You can do something like this:
MyModal::select(DB::raw("MONTH(date_col) month"))->get();
Thanks to OMR's comment this solution finally worked
$test = MyModel::selectRaw('strftime("%m", "date_col") as temp')->get();
Update: It looks like what I had in the beginning worked fine and the unit test was the only part throwing this error. Since I was working with mysql while the test environment was using sqlite which apparently doesnt like the "month()" function which OMR did point out in his comment. So this solution works in the test but not in live. Couldn't figure out how to change the test environment to use in memory mysql instead of sqlite. If anyone knows how to please add an answer here.
Hi I have written a little query which doesnt seem to working when I use Eloquent but works when I write something similar in MySQL. Data isnt being retuned which shouldnt be due to my where clauses.
I have written this query which isnt working as expected -
$invoices = Invoice::leftJoin('user_details', 'invoices.user_id', 'user_details.user_id')
->where('invoices.practice_id', '!=', 'user_details.practice_id')
->first();
It is returning data where the invoice.practice_id = user_details.practice_id
I altered the query to prove it isnt working -
$invoices = Invoice::select('invoices.practice_id', 'user_details.practice_id')
->leftJoin('user_details', 'invoices.user_id', 'user_details.user_id')
->where('invoices.practice_id', '!=', 'user_details.practice_id')
->first();
The values returned the practice_id is 6, 6 which shouldnt be possible due to the following where clause where('invoices.practice_id', '!=', 'user_details.practice_id')
Have I done something wrong which I am just not seeing?
Thank you for any help you can provide!
UPDATE
Here is the DB schema for the invoice table
Here is the the DB schema fro the user_details table
The expected result is mentioned above, no results should be returned if invoice_practice_id = user_details.practice_id
#Luke Rayner
Try by rewriting line ->leftJoin('user_details', 'invoices.user_id', 'user_details.user_id')
to
->leftJoin('user_details', 'invoices.user_id', '=', 'user_details.user_id')
If not try to print Last query and post it
Why this query is not working. Executing same thing in sql database returns me this one result that's in image. so doing q select * from kolejka where data like '2017-04-08%' in mysql works. In php artisan tinker it won't work and I cannot figure out why.
When you're using all() first, you get all data and then work with it. like won't work here. So, do this instead:
\App\Kolejka::where('data', 'like', '2017-04-08%')->get();
If you just want to get records by date, just use whereDate():
\App\Kolejka::whereDate('data', Carbon\Carbon::parse('2017-04-08'))->get();
For a search query I have the following:
DB::whereRaw('column = ?', 'foo')->orWhereRaw('column IS NULL')->get();
Adding the orWhereRaw statement gives me less results than only the whereRaw. Somehow it seems to ignore the first when adding the other. It is included in the SQL statement. Is there another way to compare for a string and null value?
I have also tried the following, as suggested below:
return self::select('id')
->where('current_state', 'unavailable')
->orWhereNull('current_state')
->get();
If I change the order (the whereNull first and the where second) this also gives me different results. It appears as if the inclusive query doesn't function correctly in correspondence with the where clause. If I use to regular where clauses I don't experience any issues.
Running SELECT * FROM events WHERE current_state='unavailable' OR current_state IS NULL; does produce the correct result for me.
Don't use whereRaw to check for null. You can use this instead:
->orWhereNull('column')
The proper way to do the first where, unless you're doing something extra such as a mysql function, is just to pass the column along like this:
where('column', '=', 'foo')
You can actually eliminate the equals, since it defaults to that. So your query would be:
DB::table('table')->where('column', 'foo')->orWhereNull('column')->get();
i have this table called dbo_modulesand it has columns ModuleCountLeft and ModuleCriticalLevel
in my controller, im trying to get the number of modules where ModuleCountLeft is less than the value of ModuleCriticalLevel. in my table, the values of ModuleCountLeft and ModuleCriticalLevel differs from row to row. so in my controller i uses this query:
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' , '<=' , DB::table('dbo_modules')->pluck('ModuleCriticalLevel') )
->count();
the problem here is that im not getting the correct values. It get the first ModuleCriticalLevel and makes it the point of comparison. for example the first ModuleCriticalLevel in the table is 20, it compares all the ModuleCountLeft to 20. Can anyone tell me what im doing wrong? or is my code wrong? please. Thanks in advance.
Try like following. You need not to pluck anything
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' ,'<=' ,'ModuleCriticalLevel')
->count();