Syntax error or access violation: 1055 Expression - mysql

I tried this:
$searchValues = explode(' ', $this->search);
$searchTermKeywords = array();
foreach ($searchValues as $word) {
$searchTermKeywords[] = " search_tags.name LIKE '%$word%'";
}
$results[0] = DB::table('search_tags')
->select('search_tags.name', 'product.*')
->join('product', 'search_tags.product_id','product.id')
->whereRaw(implode(' OR ', $searchTermKeywords))
->where([['product_quantity', '>', 0], ['active_status', '=' ,1]])
->groupBy('search_tags.product_id')
->paginate(10);
Error message:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'affiliate_new.search_tags.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL:
select count(*) as aggregate
from (select `search_tags`.`name`, `product`.*
from `search_tags`
inner join `product` on `search_tags`.`product_id` = `product`.`id`
where search_tags.name LIKE '%pen%'
OR search_tags.name LIKE '%drive%'
and (`product_quantity` > 0 and `active_status` = 1)
group by `search_tags`.`product_id`) as `aggregate_table`
) (View: C:\Users...\Desktop\my_project\resources\views\result.blade.php)

Related

Convert GROUP_CONCAT in SQL Query into Laravel Query?

this is my SQL query:
SELECT pendaftaran.id_pendaftaran,
pendaftaran.nama,
pendaftaran.nim,
pilihan_daftar.id_pilihan,
pilihan_daftar.alasan_pil,
Group_concat(dinas_biro.nama_dinasbiro) AS nama_dinas
FROM pendaftaran
JOIN pilihan_daftar
ON pendaftaran.id_pendaftaran = pilihan_daftar.id_pendaftaran
JOIN dinas_biro
ON dinas_biro.id_dinasbiro = pilihan_daftar.id_pilihan
GROUP BY pendaftaran.id_pendaftaran;
then, i convert into laravel query:
Pendaftaran::join('pilihan_daftar','pendaftaran.id_pendaftaran','=','pilihan_daftar.id_pendaftaran')
->join('dinas_biro','dinas_biro.id_dinasbiro','=','pilihan_daftar.id_pilihan')
->select(
'pendaftaran.id_pendaftaran',
'pendaftaran.nama',
'pendaftaran.nim',
'pilihan_daftar.id_pilihan',
'pilihan_daftar.alasan_pil',
DB::raw('GROUP_CONCAT(dinas_biro.nama_dinasbiro) as nama_dinas')
)
->groupBy('pendaftaran.id_pendaftaran')
->get();
But error:
SQLSTATE[42000]: Syntax error or access violation: 1055
'keanggotaan_bem.pendaftaran.nama' isn't in GROUP BY (SQL: select
pendaftaran.id_pendaftaran, pendaftaran.nama,
pendaftaran.nim, pilihan_daftar.id_pilihan,
pilihan_daftar.alasan_pil, GROUP_CONCAT(dinas_biro.nama_dinasbiro)
as nama_dinas from pendaftaran inner join pilihan_daftar on
pendaftaran.id_pendaftaran = pilihan_daftar.id_pendaftaran
inner join dinas_biro on dinas_biro.id_dinasbiro =
pilihan_daftar.id_pilihan group by pendaftaran.id_pendaftaran)
Can someone help me :(

Cannot retrieve column using eloquent

I have a query that extracts student marks, but I want to also extract the name and surname of the student but I get the error
SQLSTATE[42000]: Syntax error or access violation: 1055 'shunifu_db.users.name' isn't in GROUP BY (SQL: select name, marks.student_id,ROUND(AVG(mark)) as mark from grades inner join grades_students on grades_students.grade_id = grades.id inner join users on grades_students.student_id = users.id inner join marks on marks.student_id = grades_students.student_id where grades.stream_id = 5 and assessement_id = 1 group by marks.student_id)
here is my code
$result = DB::table('grades')
->join('grades_students', 'grades_students.grade_id', '=', 'grades.id')
->join('users','grades_students.student_id','=','users.id')
->join('marks','marks.student_id','=','grades_students.student_id')
->where('grades.stream_id', $request->stream_name)
->where('assessement_id',$request->assessement)
->select(DB::raw('marks.student_id,ROUND(AVG(mark)) as mark'))
->groupBy('marks.student_id')
->get()
May be it will work
$result = DB::table('grades')
->join('grades_students', 'grades_students.grade_id', '=', 'grades.id')
->join('users','grades_students.student_id','=','users.id')
->join('marks','marks.student_id','=','grades_students.student_id')
->where('grades.stream_id', $request->stream_name)
->where('assessement_id',$request->assessement)
->select(DB::raw('users.id,users.name,ROUND(AVG(mark)) as mark'))
->groupBy(['users.id', 'users.name'])
->get()
Here I'm using user table for grouping and also fetching name.

LARAVEL, ELOQUENT, SQL - groupBy 1

When I run the below SQL manually I get my expected results. I am trying to write the same query in Laravel's Eloquent and I get an error:
SQLSTATE[42000]: Syntax error or access violation: 1056 Can't group on 'aggregate' (SQL: select count() as aggregate from daily_menu inner join meals on daily_menu.id = meals.daily_menu_id left join fruits on fruits.meal_id = meals.id where lodge_id = 1 group by 1)*
The original SQL Query that works:
SELECT
d.id,
SUM(IF(f.id IS NULL, 0, 1)) AS fruit_count
FROM daily_menu d
JOIN meals m
ON d.id = m.daily_menu_id
LEFT JOIN fruits f
ON m.id = f.meal_id
GROUP BY 1
My Laravel Eloquent Implementation:
$query = $query
->select("daily_menu.id", DB::raw("SUM(IF(fruits.id IS NULL, 0, 1)) AS fruit_count"))
->join("meals", "daily_menu.id", "=", "meals.daily_menu_id")
->leftJoin("fruits", "fruits.meal_id", "=", "meals.id")
->groupBy(DB::raw("1"));
$query = $query
->select("daily_menu.id", DB::raw("SUM(IF(fruits.id IS NULL, 0, 1)) AS fruit_count"))
->join("meals", "daily_menu.id", "=", "meals.daily_menu_id")
->leftJoin("fruits", "fruits.meal_id", "=", "meals.id")
->groupBy(DB::raw("daily_menu.id"));
I shouldn't have used 1 I should have used the proper column alias.
Related: Can't fix this: "Cannot group by an aggregate"

Can you run a sub select query inside a raw query in laravel? If yes where am I going wrong? Here's my query

DB::table('awarded_contracts as ac')
->select(
DB::raw("COUNT(ac.id) AS numOfContracts"),
DB::raw("SUM(ac.contract_amount) AS sumOfContractAmounts"),
DB::raw("(SELECT COUNT(bid_submissions.id) from bid_submissions where bid_submissions.bid_invitation_id = ac.bid_invitation_id) AS numOfBidSubmissions"),
DB::raw("(CASE WHEN bi.procurement_method_id ='0' THEN pm.title ELSE pm2.title END) as method")
)
->join('bid_invitations as bi', function ($q) {
$q->on('bi.id', '=', 'ac.bid_invitation_id');
})
->leftJoin('procurement_plan_entries as ppe', function ($q) {
$q->on('ppe.id', '=', 'bi.procurement_plan_entry_id');
})
->join('procurement_methods as pm', function ($q) {
$q->on('ppe.procurement_method_id', '=', 'pm.id');
})
->leftJoin('procurement_methods as pm2', function ($q) {
$q->on('pm2.id', '=', 'bi.procurement_method_id');
})
->groupBy('method')
->get();
I want my subquery to count results in another table but the query is bringing syntax error issue. here's the error i get
SQLSTATE[42000]: Syntax error or access violation: 1055 'tenderpo_2019.ac.bid_invitation_id' isn't in GROUP BY (SQL: select COUNT(ac.id) AS numOfContracts, SUM(ac.contract_amount) AS sumOfContractAmounts, (SELECT COUNT(bid_submissions.id) from bid_submissions where bid_submissions.bid_invitation_id = ac.bid_invitation_id) AS numOfBidSubmissions, (CASE WHEN bi.procurement_method_id ='0' THEN pm.title ELSE pm2.title END) as method from `awarded_contracts` as `ac` inner join `bid_invitations` as `bi` on `bi`.`id` = `ac`.`bid_invitation_id` left join `procurement_plan_entries` as `ppe` on `ppe`.`id` = `bi`.`procurement_plan_entry_id` inner join `procurement_methods` as `pm` on `ppe`.`procurement_method_id` = `pm`.`id` left join `procurement_methods` as `pm2` on `pm2`.`id` = `bi`.`procurement_method_id` left join `procurement_types` as `pt` on `pt`.`id` = `ppe`.`procurement_type_id` where `ac`.`financial_year` = 2018-2019 group by `method`)

Join ONE column to another table in a SELECT Statement

I am trying to pull the 'name' column from a table called ps_product_lang. My current SQL statement without the JOIN is:
$stmt1 = $conn->prepare("SELECT reference, price FROM ps_product_attribute WHERE id_product = ". implode(' AND ', $_POST['checkbox']) ."");
Here is my Statement with the join:
$stmt1 = $conn->prepare("SELECT * FROM ps_product.id_product as id_product, reference, price, name LEFT JOIN ps_product_lang ON ps_product_lang.id_product = ps_product.id_product WHERE id_product = ". implode(' AND ', $_POST['checkbox']) ."");
The error I am getting is:
Error: SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT
command denied to user 'removed'#'localhost' for table 'id_product'
Why is this?