I'm new to Laravel, I wanted to make a union with the DB and it didn't work for me, I tried it in SQL and it worked fine, I don't know what I'm doing wrong?
Would you be kind enough to help me with this dilemma, thanks
here is the SQL query:
SELECT SUM(materias.Credito) FROM historico_notas INNER JOIN materias ON materias.id = historico_notas.materias_id INNER JOIN estudiante_seccion on estudiante_seccion.id = historico_notas.estudiante_seccion_id WHERE historico_notas.nota >= 10 and estudiante_seccion.id_persona = 627;
This is the generated query SQL:
here in my attempt in laravel:
$prueba = DB::table('historico_notas')
->join('materias','materias.id', '=',' historico_notas.materias_id ' )
->join('estudiante_seccion',' estudiante_seccion.id','=','historico_notas.estudiante_seccion_id' )
->where('historico_notas.nota','>=',10 )
->where(' estudiante_seccion.id_persona','=',$persona_alumno)
->sum('materias.Credito');
This is the generated query and error.
My dilemma is that I want my sql query to Laravel and something gives me an error and I don't know what error I have
the SQL query works perfectly for me, but when I try to go to Laravel it gives me an error,
what i show is my attempt to pass the sql query to laravel and what i want is my sql query to laravel
How would the syntax be properly?
I just converted your SQL into this.
$query = DB::table('historico_notas')
->join('materias', 'materias.id', '=', 'historico_notas.materias_id')
->join('estudiante_seccion', 'estudiante_seccion.id', '=', 'historico_notas.estudiante_seccion_id')
->where('historico_notas.nota', '>=', 10)
->where('estudiante_seccion.id_persona', '=', 627)
->sum('materias.Credito');
The error gets an unknown column "historico_notas.nota", you can check it in the table if it exists
Related
This SQL:
SELECT COUNT(*) AS total_see_all_video
from users u
WHERE 7 = (SELECT COUNT(*) FROM lecciones_users lu WHERE lu.uuid = u.uuid)
I tried this code but did not work:
$data = LeccionesUsers::select(
DB::raw('COUNT(*) AS total_ase_vis_videos'),
DB::raw('where 7 = (SELECT COUNT(*) FROM lecciones_users where leccion_users.uuid = users.uuid)')
)
->join('users', 'lecciones_users.uuid', '=', 'users.uuid')
->get();
Your issue is that you are not correctly forming your query. You can do ->toSql(); instead of ->get(); and you would see the final SQL (would definitely not be the same as the one you wrote first).
So, you should have this to have the same SQL:
$total_see_all_video = LeccionesUsers::whereRaw('7 = (SELECT COUNT(*) FROM lecciones_users where leccion_users.uuid = users.uuid)')
->count();
Please, try my query (and also run ->toSql() to see if you have a correct SQL).
I would still recommend to use relationships and it is very weird to do 7 = query.
You can use
DB::query()->fromSub('Raw sql query here..')
and then can perform actions on this.For the reference you can use the documentation fromSub
You can also look into this convert this where break this query to parts to be used accordingly. You can use this section for the reference purpose:
Laravel-Raw-Expressions
Hope this will help you with the result.
I have this code:
$abcd= DB::table('control')
->leftJoin('proces', 'proces.id_control', '=', 'control.id_control')
->select('control.id_risk', DB::raw("group_concat(proces.id_procedur) as prc"))
->where('control.id_theme', '=', $id)
->whereExists(function ($query) {
$query->select('id_control')
->from('proces') ->where('control.id_control','proces.id_control');
})
->groupBy('control.id_risk')
->get();
This query work perfectly, when i wrote it in phpmyadmin, but when I try to execute it in app query return empty array. Where is problem
UPDATE
After i check how looks query from laravel there is one wrong
select `control`.`id_risk`, group_concat(proces.id_procedur) as prc from `control` left join `proces` on `proces`.`id_control` = `control`.`id_control` where `control`.`id_theme` = ? and exists (select `id_control` from `proces` where `control`.`id_control` = ?) group by `control`.`id_control`
First question mark is corrent beacue this $id(by the way variable is passing good value) but the second question mark should not be there.
I have a problem to make a query in Laravel
My query in POSTGRESQL is:
SELECT con.id, con.vehiculo_id, con.cre, ve.chofer_id, ve.placa
FROM conciliaciones as con
JOIN vehiculos as ve
on con.vehiculo_id = ve.id or con.vehiculo_id IS NULL or con.vehiculo_id IS NOT NULL;
The problem with the query is that the plate is repeated when there should only be 1 single row with the data plate the rest is null
I have the following code, but it does not work:
$conciliacion = DB::table('conciliaciones')
->join('vehiculos','conciliaciones.vehiculo_id','=','vehiculos.id')
->select('conciliaciones.*','vehiculos.placa')
->whereNull('conciliaciones.vehiculo_id')
->whereNotNull('conciliaciones.vehiculo_id')
->get();
How can I make the query?
In the query, select other columns but is Similarly that I need in Laravel
thanks
my solution xD
$conciliacion = DB::table('conciliaciones')
->leftJoin('vehiculos','vehiculos.id', '=', 'conciliaciones.vehiculo_id')
->select('conciliaciones.*','vehiculos.placa')
->orderBy('conciliaciones.id')
->get();
I'm using the following code to create a query with a join.
$query = rs_vehicles::select(
'rs_vehicles.id',
'rs_vehicles.type',
'rs_vehicles.make',
'rs_vehicles.model',
'rs_vehicles.year',
'rs_vehicles.status',
'rs_vehicle_regs.registration'
);
$query ->leftJoin('rs_vehicle_regs', function($join){
$join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id')
->where('rs_vehicle_regs.registration_date', '=',
DB::raw("(select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)"));
});
$rsGrid = $query->get();
This produces the following sql statement:
select rs_vehicles.id, rs_vehicles.type, rs_vehicles.make,
s_vehicles.model, rs_vehicles.year, rs_vehicles.status,
s_vehicle_regs.registration from rs_vehicles
left join rs_vehicle_regs
on rs_vehicle_regs.rs_vehicles_id = rs_vehicles.id
and rs_vehicle_regs.registration_date = (select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)
The SQL statement generated by Laravel runs perfect when I execute it in MySQL Workbench and it brings back the expected values for ALL fields. However when I execute the code in Laravel although the script runs perfectly with no errors and brings back almost all the required data the 'rs_vehicle_regs.registration' field which is in the joined table comes back as null. I've spent ages trying to figure this out and I am getting nowhere. Anybody any idea why this wont work in Laravel? I'm at my wits end.
I have tried changing get() to toSql() and I get the following
select `rs_vehicles`.`id`, `rs_vehicles`.`type`, `rs_vehicles`.`make`, `rs_vehicles`.`model`, `rs_vehicles`.`year`, `rs_vehicles`.`vehicle_status`, `vh_type`.`display_text` as `type_text`, `vh_status`.`display_text` as `vehicle_status_text`, `rs_vehicle_regs`.`registration` as `registration` from `rs_vehicles` left join `app_params` as `vh_type` on `rs_vehicles`.`type` = `vh_type`.`list_value` and `vh_type`.`param_type` = ? left join `app_params` as `vh_status` on `rs_vehicles`.`vehicle_status` = `vh_status`.`list_value` and `vh_status`.`param_type` = ? left join `rs_vehicle_regs` on `rs_vehicle_regs`.`rs_vehicles_id` = `rs_vehicles`.`id` where `rs_vehicles`.`status` = ?
I have also tried the following:
$query ->leftJoin('rs_vehicle_regs', function($join){
$join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_employees.rs_vehicles_id')
->where('rs_vehicle_regs.registration_date', '=', 'max(registration_date)');
});
This produces the exact same result as the original query where the object as the field but the values are null.
Then I tried this:
$query ->leftJoin('rs_vehicle_regs', 'rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id');
This join works and brings back the data I am looking for however if the join table has more than one entry related to the parent table as can happen I get all records returned and I only want one record were max(registration_date). This has me totally stumped.
I've tried creating this query in Laravel and still can't get it to work with a query builder. It's giving me syntax errors yet I can run it in the tables and it run smooth.
Query trying to run:
select A.*, sum(RawAmt) as AmountOwed from (select Login, PatID,
if(length(ApptID) > 0, ApptID, VisitID) as VisitID,
ServiceDate,
TotalCharge,
InsurancePaid,
PrevPaid,
WriteOff,
Refund,
MiscDebit,
AmountOwed as RawAmt,
ApptTime,
ApptDate,
Physician,
isCopay,
HLocation from MDPay_AcctHist where Login='demo') A
group by PatID, VisitID
It's giving me a syntax issue when trying to do this with DB::raw statements in DB::select and DB::where;
Any help on trying to write this to meet laravels specs would be helpful.
$subquery = DB::selectRaw('
Login, PatID,
if(length(ApptID) > 0, ApptID, VisitID) as VisitID,
ServiceDate,
TotalCharge,
InsurancePaid,
PrevPaid,
WriteOff,
Refund,
MiscDebit,
AmountOwed as RawAmt,
ApptTime,
ApptDate,
Physician,
isCopay,
HLocation')
->from('MDPay_AcctHist')
->where('Login', '=', 'demo')
->toSql();
$result = DB::selectRaw('A.*, sum(RawAmt) as AmountOwed')
->from(DB::raw($subquery . ' as A'))
->groupBy('PatID', 'VisitID')
->get();