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 :(
Related
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)
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.
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"
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?
I have a function for prices with 2 joins and it works fine and dandy when I use it to call for prices. But now when I added Kaminari to be able to paginate, it gave me a error and I wonder if I can customize the "count*" function in Kaminari or is it my function for calling the products that's wrong?
This is in my controller
#prices = Price.select("`prices`.*").joins(:retailer, :retailer => :profile).
where(['product_id=? AND size_id=?', params[:prod_id], params[:si_id]]).
group(:retailer_id).order("SUM((prices.price * #{params[:amount].to_i}) + profiles.shippingCost)").page(params[:page])
Error code:
Mysql2::Error: Column 'retailer_id' in field list is ambiguous: SELECT COUNT(*) AS count_all, retailer_id AS retailer_id FROM `prices` INNER JOIN `retailers` ON `retailers`.`id` = `prices`.`retailer_id` INNER JOIN `profiles` ON `profiles`.`retailer_id` = `retailers`.`id` WHERE (product_id='1' AND size_id='3') GROUP BY retailer_id,retailer_id ORDER BY SUM((prices.price * 1) + profiles.shippingCost)
When I call the products without Kaminari the MYSQL looks like this.
SELECT `prices`.* FROM `prices` INNER JOIN `retailers` ON `retailers`.`id` = `prices`.`retailer_id` INNER JOIN `profiles` ON `profiles`.`retailer_id` = `retailers`.`id` WHERE (product_id='1' AND size_id='3') GROUP BY retailer_id ORDER BY SUM((prices.price * 1) + profiles.shippingCost)
Thanks in advance!
If ambiguous error then try explicit syntax for retailer_id.
e.g. retailers.retailer_id ,
profiles.retailer_id ,
prices.retailer_id.
ray
:retailer is duplicated inside your join clause.
Please change your join params to
.joins(:retailer => :profile)