Yii2 - Syntax error or access violation: 1066 Not unique table - yii2

I'm trying to join two tables in a query :
- sc_cours -
idCour
volHoraireCour
idMat
- sc_matieres -
idMat
nomMat
the code of the query is the following
$query->select('*')
->from('sc_cours')
->innerJoin('sc_matieres', 'sc_cours.idMat = sc_matieres.idMat');
But i get this following error
SQLSTATE[42000]: Syntax error or access violation: 1066 Table/alias: 'sc_matieres' non unique
The SQL being executed was: SELECT * FROM `sc_cours` INNER JOIN `sc_matieres` ON sc_cours.idMat = sc_matieres.idMat INNER JOIN `sc_matieres` ON sc_cours.idMat = sc_matieres.idMat
Do you know what's the main problem guys? Thanks you!

From error you can see that you have two inner joins exact same. Define relation in model and than just do ->innerJoin('nameOfRelation');

Ok, with ->ScCours::find()->join('mat') I've this error Missing argument 2 for yii\db\Query::join() , But Thank you, you put me on the right path because it works when i try with with , so i've just done ->ScCours::find()->with('mat'). That's worked now, thank you again Borisa Eric for your precious help !

Related

mySQL pre-8.x Stack Overflow 'solution' not working?

Hello and thank you for any help you can provide.
I am running on a shared-server platform so I am restricted to using mySQL version 5.6.46 which means I do not have access to "recursive CTE" and "WITH" commands . After many hours of looking for a solution for how I can perform a recursive-type operation - and after trying many approaches that did not end up working - I found this Stack Overflow reference to an approach that seems like a reasonable method for what I am trying to accomplish (and simple enough for someone at my level of understanding). The method described can be found about 1/4 of the way down the page under the title of "Alternative 3: Repeated Self-joins."
In essence, I have copied the entire recommended mySQL query and modified it to my table structure BUT it triggers an error and does not run.
The error is "#1054 - Unknown column 'p2.id' in 'on clause'" which - according to my internet searches - is a common error with many references BUT each refers refers to a two-table structure with a 'comma' that - based on a SQL update years ago now requires parentheses to make it work. I am puzzled with this error on my end because I DO NOT have a two-table structure. I can find no other reason for this error. In fact, on the https://www.piliapp.com/ syntax checker it shows the syntax is fine. I would appreciate your eyes on this query to see what I'm missing. Here is my code:
select p4.ManagerID as parent4_id,
p3.ManagerID as parent3_id,
p2.ManagerID as parent2_id,
p1.ManagerID as parent_id,
p1.EmployeeID as employee_id,
p1.LastName
from cps_db_sm7.dEmployees p1
left join cps_db_sm7.dEmployees p2 on p2.id = p1.ManagerID
left join cps_db_sm7.dEmployees p3 on p3.id = p2.ManagerID
left join cps_db_sm7.dEmployees p4 on p4.id = p3.ManagerID
where 23578 in (p1.ManagerID,
p2.ManagerID,
p3.ManagerID,
p4.ManagerID)
ORDER BY 1,2,3,4,5;
The error is triggered by the first LEFT JOIN and is in reference to the ON p2.id. Could someone please look at this and suggest a solution? Thank you!

ER_BAD_FIELD_ERROR: Unknown column 'table.column' in 'on clause'

i'm getting a really confused problem... i'm triying to do a query:
SELECT ordenes_servicio.idorden, estatus_orden.descripcion AS estatus, tipo_orden.descripcion AS tipo_orden, usuario.nombre, ordenes_servicio.nombres_cliente, ordenes_servicio.fecha_asig
FROM ordenes_servicio
INNER JOIN estatus_orden ON ordenes_servicio.idestatus_orden = estatus_orden.idestatus_orden
INNER JOIN tipo_orden ON ordenes_servicio.idtipo_orden = tipo_orden.idtipo_orden
INNER JOIN usuario ON ordenes_servicio.id = usuario.id
ORDER BY ordenes_servicio.idorden DESC
The problem is that when i try to run it, it say
ER_BAD_FIELD_ERROR: Unknown column 'ordenes_servicio.idusuario' in
'on clause'
But if you look at the query, there is no 'ordenes_servicio.idusuario', and yes there was, but i update my tables, both of them, and it is like the query is still triying to get that column.
When i execute the query in my MySQL Workbench it work, but dont when i try to run it in my application... Someone can help me? :/
Note: my "app" is actually a REST API in typescript, and i'm using express library
I got the answer... if can help a beginner like me someday. Im actually executing three querys, and there was one of then that still had "ordenes_servicio.idusuario"... that's all.. So check your querys!
Beginner error...

Syntax error or access violation: 1055 'wockhardt_indigo.stock_levels.id' isn't in GROUP BY

I've recently started getting an error with the below statement
Model::select(DB::raw('*, SUM(`unrestrict`) as sumUnrestrict, SUM(`quarantine`) as sumQuarantine'))->with(['materialDescription'])->orderBy('mat_no')->groupBy('mat_no')->get();
The error is
Syntax error or access violation: 1055 'wockhardt_indigo.stock_levels.id' isn't in GROUP BY
Any idea to solve? TIA
You must assign explictally the column you need and these must match teh column in group by eg:
Model::select(DB::raw('mat_no,
SUM(`unrestrict`) as sumUnrestrict,
SUM(`quarantine`) as sumQuarantine'))->
with(['materialDescription'])->
orderBy('mat_no')->
groupBy('mat_no')->get();
Seems to be an issue within the Laravel Framework. Laravel 5.3 ships with the strict mode in SQL turned on which doesn't work great with Eloquent, changing to false in config/database.php solves the solution and brings back the 5.2 behaviour.

MySQL error 1064 - what is causing it?

I have two similar queries, and the second one is throwing a 1064 error and I can't figure out why. Do you see the issue?
select * from node
join field_data_field_taxonomytopics as tt
on node.nid = tt.entity_id
where tt.bundle = 'magazine_article' and tt.entity_id = 61928;
SELECT
FROM
node node
INNER JOIN field_data_field_taxonomytopics field_data_field_taxonomytopics ON node.nid = field_data_field_taxonomytopics.entity_id
WHERE (field_data_field_taxonomytopics.bundle = 'magazine_article') AND (field_data_field_taxonomytopics.entity_id = '61928')
TL;DR Error #1064 means that MySQL can't understand your command. To fix it:
Read the error message. It tells you exactly where in your command
MySQL got confused. Check the manual. By comparing against what MySQL
expected at that point, the problem is often obvious. Check for
reserved words. If the error occurred on an object identifier, check
that it isn't a reserved word (and, if it is, ensure that it's
properly quoted).
I am guessing the issue here is that in the FROM clause you wrote the table name twice "node node", in addition, you are not selecting anything.
SELECT *
FROM
node node
I hope that will fix the error.

Mysql issue pertaining to syntax error on Not IN

select tf.Id,tf.Name,tf.LName,tf.Rank,tf.Category
where tf.Id not in (select fighter_id from tbl_player_fighter where league_id=91)
and tf.Status ='1'
This is throwing a syntax error. Any advice on how to fix this. Thanks.
You are missing a FROM clause, so it doesn't know the main table to use.