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?
Related
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 :(
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.
I would like to SUM the foglalas_mennyiseg field in the foglalas table.
With the code below, i get this error: Notice: Undefined index: foglalt
What am i doing wrong?
$sql =
"
SELECT
rendeles.rendeles_id,
rendeles.rendeles_gyarto,
rendeles.rendeles_termek,
rendeles.rendeles_mennyiseg,
rendeles.rendeles_szam,
rendeles.rendeles_status,
rendeles.rendeles_created,
rendeles.rendeles_visszaig,
gyarto.gyarto_nev,
termek.termek_nev,
termek.termek_egyseg,
(SELECT SUM(foglalas.foglalas_mennyiseg) AS foglalt FROM foglalas
WHERE foglalas.foglalas_rendeles_id = rendeles.rendeles_id)
FROM rendeles
LEFT JOIN gyarto ON rendeles.rendeles_gyarto = gyarto.gyarto_id
LEFT JOIN termek ON rendeles.rendeles_termek = termek.termek_id
ORDER BY rendeles_id DESC LIMIT $actual, $row_per_page
";
You need your name after your subquery
....
( SELECT SUM(foglalas.foglalas_mennyiseg)
FROM foglalas
WHERE foglalas.foglalas_rendeles_id = rendeles.rendeles_id
) AS foglalt
....
I have a sql query like below -
SELECT
district, coalesce(sell.sale,0) as totalsale
FROM `districts`
left join
(SELECT parties_district, billdate, sum(billamount) as sale FROM `bills` left join parties on bills.bills_partyname = parties.parties_partyname group by parties_district) as sell
on sell.parties_district = districts.district
So far I've written the query till -
SELECT
parties_district, billdate, sum(billamount) as sale
FROM `bills`
left join parties
on bills.bills_partyname = parties.parties_partyname
group by parties_district
And my query in yii2 ActiveRecord looks like -
$query = Parties::find()
->select(['parties_district','parties_partyname','sum(billamount) as sale'])
->joinWith('bills')
->groupby('parties_district');
Please tell me how can I write the whole query. I think I'm stuck with the alias part and there is a subquery in it. Please tell me how can I write a subquery within a query.
I've tried -
$subQuery = Parties::find()->select(['parties_district','parties_partyname','sum(billamount) as sale'])->joinWith('bills sell')->groupby('parties_district');
$query = Districts::find()->select(['district','coalesce(sell.sale,0) as totalsale'])->leftJoin('$subQuery', 'sell.parties_district = districts.district');
$models = $query->all();
but getting the following error
Database Exception – yii\db\Exception
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'gm.$subquery' doesn't exist
The SQL being executed was: SELECT `district`, coalesce(sell.sale,0) as totalsale FROM `districts` LEFT JOIN `$subQuery` ON sell.parties_district = districts.district
Error Info: Array
(
[0] => 42S02
[1] => 1146
[2] => Table 'gm.$subquery' doesn't exist
)
↵
Caused by: PDOException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'gm.$subquery' doesn't exist
In this case is better use a literal notation for select content (and not hash notation) this way
$query = Parties::find()
->select('parties_district, parties_partyname, sum(billamount) as sale'])
->joinWith('bills')
->groupby('parties_district');
and for more complex query use findBySql Query
$sql = "SELECT
district, coalesce(sell.sale,0) as totalsale
FROM `districts`
left join
(SELECT parties_district, billdate, sum(billamount) as sale
FROM `bills` left join parties
on bills.bills_partyname = parties.parties_partyname
group by parties_district) as sell
on sell.parties_district = districts.district"
$model = Districts::findBySql($sql)->all();
I've figured out the answer. Thanks to this page - www.yiiframework.com/doc-2.0/guide-db-query-builder.html.
It will be like below -
$subQuery1 = (new Query())->select(['parties_district','billdate','sum(billamount) as sale'])->from ('bills')->join('LEFT JOIN','parties','bills.bills_partyname = parties.parties_partyname')->groupby('parties_district')->where('billdate != "NULL"');
$query = (new Query())->select(['district','coalesce(sell.sale,0) as totalsale'])->from('districts')->leftJoin(['sell' => $subQuery1],'sell.parties_district = districts.district');
I'm trying this:
select
audit_log_entries.created_at,
audit_log_orig_term_types.name as originator,
audit_log_orig_term_types.name as terminator
from audit_log_entries
join audit_log_orig_term_types on audit_log_entries.originator_type_id = audit_log_orig_term_types.id
join audit_log_orig_term_types on audit_log_entries.terminator_type_id = audit_log_orig_term_types.id;
I think the intent is clear, I want both names for the originator and terminator. I have their IDs in the first table and the names on the other table.
I am getting an error from this: ERROR 1066 (42000): Not unique table/alias: 'audit_log_orig_term_types'
where's the mistake in the syntax?
You can make like this :
select
audit_log_entries.created_at,
audit1.name as originator,
audit2.name as terminator
from audit_log_entries
join audit_log_orig_term_types audit1 on audit_log_entries.originator_type_id = audit1.id
join audit_log_orig_term_types audit2 on audit_log_entries.terminator_type_id = audit2.id;
You need to alias the tables:
join audit_log_orig_term_types AS alias1 on audit_log_entries.originator_type_id = alias1.id
^^^^^^^^^ ^^^^^^
join audit_log_orig_term_types AS alias2 on audit_log_entries.terminator_type_id = alias2.id;
Use an alias for the joined tables:
Join table1 as t1 on t1.Id = [...]