I'm little wonder please help me out.
My query is :- Invoice::join('orders', 'orders.invoice_id', '=', 'invoices.id')->groupBy('invoices.id')->get();
Then I have got an error: Syntax error or access violation: 1055 'invoices.reference_number' isn't in GROUP BY
When I updated my query according to error then the query is:-
$invoices = Invoice::join('orders', 'orders.invoice_id', '=', 'invoices.id')->groupBy('invoices.id','invoices.reference_number','invoices.customer_address_id','invoices.country_id','invoices.paid_at','invoices.due_date','invoices.created_at','invoices.updated_at','invoices.deleted_at','orders.id','orders.currency_id','orders.user_id','orders.customer_id','orders.created_at','invoices.updated_at')->get();
Now query working but in that case, I can't use select(), It returns columns who is in the groupBy()
So I did google then found if I do database strict mode off then it will work and then I did and it's working good, But I don't want strict mode turned off.
Also, i did the study about database modes https://dev.mysql.com/doc/refman/5.5/en/faqs-sql-modes.html https://dev.mysql.com/doc/refman/5.5/en/sql-mode.html
Then I found query:- SELECT ##GLOBAL.sql_mode; SELECT ##SESSION.sql_mode;
But the result is blank
If I do in the database - select * from invoices inner join orders on orders.invoice_id = invoices.id where invoices.deleted_at is null group by invoices.id
Then it's working fine means no mode validations trough mySQL it has configured side of Laravel
Also, i haven't found related database modes in laravel documentation
So help me out if I did wrong something.
And i also want to know who's database strict mode validation is enabled in laravel because I have wonder what strict validation giving me an error.
Change this line in mysql array of config/database.php
'strict' => true,
To
'strict' => false,
Where this occurs add this top of the function
public function index() {
DB::statement("SET sql_mode = '' ");
# rest of your Joins
}
run this query after setting connection in database file
SET sql_mode = false;
Related
I was trying to run this query in laravel:
$siswa = DB::table('siswa')
->join('pengambilankelas', 'siswa.id_siswa', '=', 'pengambilankelas.id_siswa')
->join('subcpmkpengambilan', 'pengambilankelas.id_pengambilanKelas', '=', 'subcpmkpengambilan.id_pengambilanKelas')
->join('tesformatif', 'subcpmkpengambilan.id_subcpmkpengambilan', '=', 'tesformatif.id_subcpmkpengambilan')
->select('siswa.*', 'tesformatif.*', DB::raw('max(tesformatif.nilai_tesFormatif) as max_nilai'))
->groupBy('siswa.id_siswa')
->where('pengambilankelas.id_kelas','=', $id_kelas)
->where('subcpmkpengambilan.id_subCPMK', '=', $currentSubcpmk->id_subCpmk)
->where('subcpmkpengambilan.status_subcpmkpengambilan', '=', 3)
->get();
then i got this error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'dil.siswa.identitas_siswa' isn't in GROUP BY (SQL: select `siswa`.*, `tesformatif`.*, max(tesformatif.nilai_tesFormatif) as max_nilai from `siswa` inner join `pengambilankelas` on `siswa`.`id_siswa` = `pengambilankelas`.`id_siswa` inner join `subcpmkpengambilan` on `pengambilankelas`.`id_pengambilanKelas` = `subcpmkpengambilan`.`id_pengambilanKelas` inner join `tesformatif` on `subcpmkpengambilan`.`id_subcpmkpengambilan` = `tesformatif`.`id_subcpmkpengambilan` where `pengambilankelas`.`id_kelas` = 5 and `subcpmkpengambilan`.`id_subCPMK` = 6 and `subcpmkpengambilan`.`status_subcpmkpengambilan` = 3 group by `siswa`.`id_siswa`)
Query run fine outside laravel. this post tell me to disable strict mode
and it works.
is there a better way without disabling strict mode? is it even save to do so?
ps. I'm sorry the database is in indonesian.
You may have the sql_mode ONLY_FULL_GROUP_BY enabled. To disable it run in the mysql console the following command:
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
References
Disable ONLY_FULL_GROUP_BY
I'm Running the following query without any problem in MySQL (5.7.21) Database but this query throws error on my hosting server database which is MariaDB (10.1.47-MariaDB) and I don't have super permission to change the sql mode.
$projects = V2Project::leftJoin('v2_project_locations as v2pl', 'v2pl.project_id', 'v2_projects.id')
->leftJoin('v2_locations as v2l', 'v2l.id', 'v2pl.location_id')
->selectRaw('v2_projects.id, v2_projects.name, group_concat(v2l.name) as locations')
->withDepartmentInMind()
->onlyAuthorizedObjects();
if($by_current_step == true) {
$projects = $projects->where('current_step', 'plan');
}
if($search_val) {
$projects = $projects->whereIn('v2_projects.id', $search_val);
}
$projects = $projects->groupBy('v2_projects.id')->paginate(10);
$projects->appends(Input::except('page', '_token'));
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'pm_form_moe.v2_projects.name' isn't in GROUP BY (SQL: select v2_projects.id, v2_projects.name from `v2_projects` group by `v2_projects`.`id`)
I also tried to add every column, but I need the functionality the same as mySQL without adding all columns.
Read about ONLY_FULL_GROUP_BY, then decide what to remove from your SELECT or what to add to the GROUP BY. Don't worry about setting the flag; your code is bad and needs to be fixed. And it was bad in the older version when it did not complain.
(If you show us the generated SQL, we can discuss it further.)
I dont want to believe there is a bug in laravel 5.5 cos i am amazed how i keep getting errors on the Eloquent Query Builder when ever i use the groupBy in my query.
Tried converting this SQL that works seamlessly to Eloquent and i keep getting Errors on the groupBy..
Had a similar issue with an SQL which i complained here and till now i have not gotten a perfect answer for it
open issue here
SELECT * FROM arm_articles
INNER JOIN arm_interest ON arm_articles.article_category = arm_interest.category_id
WHERE arm_articles.article_contributor_id='1322'
GROUP BY arm_articles.article_id
ELOQUENT VERSION THAT RETURNS ERROR
$contributor_id =1322;
DB::table('arm_articles')
->join('arm_interest', function($join) {
$join->on('arm_articles.article_category','=','arm_interest.category_id');
})
->having('arm_articles.article_contributor_id', '=', $contributor_id)
->groupBy('arm_articles.article_id')->get()
ERROR MESSAGE
SQLSTATE[42000]: Syntax error or access violation: 1055 'knowledge_db.arm_articles.id' isn't in GROUP BY (SQL: select * from 'arm_articles' inner join 'arm_interest' on 'arm_articles'.'article_category' = 'arm_interest'.'category_id' group by 'arm_articles'.'article_id' having 'arm_articles'.'article_contributor_id' = 1322)
So like i said b4 its really weared why i will get the error in the first place as adding ->toSql to the query returns the right SQL.
Add this configuration to your mysql database config in config/database.php:
'mysql' => [
....
'strict' => false,
//'strict' => true,
],
Edit sql_mode in your mysql config file (my.ini or my.cnf)
[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Restart your MySQL
I am using Laravel 4.2 and am calling a Stored procedure from a database on the server. Locally it works fine, but on the server, using the same DB, it gives error. The call is as follows (I just want to make select):
$result = DB::select('CALL sp_special_prices("'.$codClient.'", "'.$codProduct.'", "'.$quantity.'", "'.$grup.'", "'.$FirmCode.'")');
When running a product listing on the server, it works except for two articles, giving the following error:
Caught exception: SQLSTATE[HY000]: General error: 2053 (SQL: CALL sp_special_prices("C000000", "445706049", "1", "146", "75");)
I already checked the php versions, and am using 5.6 on both sites. Could it be some special configuration? Is that the DB is the same, the place to call the SP, is that it is different.
If the procedure doesn't return anything, you need to use DB::statement instead of DB::select. If it does return something, you need to use DB::select
If the procedure has variable behaviour (may or may not return data depending on input), I suggest you change the procedure to return some data for all input combinations
I have a database that has a few stored procedures in it that I would like to call via CodeIgniter. I have the following code in my Model class:
$sql = "CALL `stored_proc`(1)";
$query = $this->db->query($sql); //This call breaks the DB :(
$this->db->select('status');
$this->db->where('id', $id);
$query = $this->db->get('table');
print($query->num_rows()); //line 1116
When I run this code, I get the following error:
Fatal error: Call to a member function num_rows() on a non-object in C:\server\apache\htdocs\application\models\let_model.php on line 1116
If I remove the query line, the select works properly. Also, if I replace the call to a stored procedure with say a SELECT command, it also works properly.
Is there something obvious I'm missing for why I'm getting this error? If there isn't a good answer, is there a way to work around this problem?
Thanks for your time!
Edit: After delving a little deeper into the problem, it seems that this error will occur if my stored procedure contains a SELECT command. UPDATES seem to work properly. Perhaps this problem has something to do with how CodeIgniter deals with SELECT results?
Since this question appears first on google, i've managed to solve this by using :
$this->db->simple_query($query);
Instead of the regular query function.
The problem was that the SELECTs in the stored procedure were causing problems in CodeIgniter. By making sure that all of the SELECTs were directed (i.e. with the INTO clause), the stored procedure was able to run successfully.
For example,
-- This causes an error in CodeIgniter when inside a stored procedure.
SELECT GET_LOCK('lock1',0);
-- This does not.
DECLARE _lock_result INT;
SELECT GET_LOCK('lock1',0) INTO _lock_result;
I am still unaware of the underlying causes of why this causes an error, but this solution will suffice for my current work.
use only
$query->num_rows instead of $query->num_rows()