Hello I have been getting Getting unknown property: app\models\ActiveCurriculum::period the period column is located at the schead table
I have used this code to join the tables.
ActiveCurriculum::find()
->select('scstock.*')
->joinWith('schead')
->where(['schead.TrNo' => $TrNo])
->one();
Can you help me?
EDIT 1 (fixed)
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE schead.TrNo='29005'' at line 1
The SQL being executed was: SELECT scstock.* FROM scstock LEFT JOIN schead WHERE schead.TrNo='29005'
Edit 2: I managed to fix it but I am getting a new error again
"A join clause must be specified as an array of join type, join table, and optionally join condition."
ActiveCurriculum::find()
->select(`scstock.*`)
->leftJoin(`schead`)
->where([`schead.TrNo` => $TrNo])
->one();
ActiveCurriculum::find()->select('scstock.*')
->joinWith('schead')
->where(['schead.TrNo' => $TrNo])
->one();
// here joinWith('schead') means `schead` is name of the class
ActiveCurriculum::find()->select('scstock.*')
->joinLeft('schead','schead.scheadid=scstock.scheadid') // scheadid is the common field between two fields
->where(['schead.TrNo' => $TrNo])
->one();
Related
Am running a query builder on my laravel app using inner join
But Laravel keeps throwing errors, i tested the MySql Query
i wrote directly in my database using phpMyAdmin it works fine
SELECT
u.id, u.first_name, u.last_name, u.username, u.sponsor,u.deleted_at, i.id as investment_id, i.total_amount, i.created_at
FROM
users AS u
JOIN investments as i ON
i.id= ( SELECT i1.id FROM investments as i1 where u.id=i1.user_id and i1.status ='confirmed' ORDER BY i1.created_at LIMIT 1)
WHERE
u.sponsor = '901d08da-e6c4-476a-ae7b-9386990b8b9e' AND u.deleted_at is NULL
ORDER BY
created_at DESC
but when i write it using query builder it wont work.
$refered_users = DB::table('users')
->join('investments', function ($join) {
$join->on('users.id', '=', 'investments.user_id')
->where('investment.status', '=', 'confirmed')
->orderBy('investment.created_at','desc')->first();
})
->select('users.id,users.first_name,users.last_name,users.username,users.sponsor,investment.id AS investment_id,investment.total_amount,investment.created_at')
->where('users.sponsor','=',Auth::User()->id)
->orderBy('investment.created_at','desc')
->paginate(10);
i tried a RAW DB::select() and it works but i want to use the query builder so i can paginate the results.
this is how my table is arranged:
users
id, first_name, last_name, username, sponsor(id of another user), created_at, deleted_at
investments
id, total_amount , user_id(id of a user), status, created_at, deleted_at
Am not much Good with SQL Queries so if am writing it all wrong please don't scold just, try to explain a lil bit more so i can understand
this is the error coming out:
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'on users.id = investments.user_id and
investment.status = ? order by' at line 1 (SQL: select * on
users.id = investments.user_id and investment.status =
confirmed order by investment.created_at desc limit 1)
output of the dd
select first_name,last_name,username,deleted_at,total_amount,
investment_created_at,user_id from `users`
inner join (select user_id,total_amount,status,created_at AS investment_created_at from `investments`
where `status` = ? and `investments`.`deleted_at` is null) as `confirmed_investments`
on `users`.`id` = `confirmed_investments`.`user_id`
where `sponsor` = ? order by `investment_created_at` desc ◀`
`array:2 [▼ 0 => "confirmed" 1 => "901d08da-e6c4-476a-ae7b-9386990b8b9e" ]`
am using:
PHP version: 7.3.1
MySql Version: 8.0.18
You can get query of your code and compare it to your desired query like this:
$refered_users = DB::table('users')
->join('investments', function ($join) {
$join->on('users.id', '=', 'investments.user_id')
->where('investment.status', '=', 'confirmed')
->orderBy('investment.created_at','desc')->first();
})
->select('users.id,users.first_name,users.last_name,users.username,users.sponsor,investment.id AS investment_id,investment.total_amount,investment.created_at')
->where('users.sponsor','=',Auth::User()->id)
->orderBy('investment.created_at','desc')
->toSql();
Your join query is getting data for id by running another query. I think you need to use sub query. If you are using laravel > 6 it is on documentation.
I've got a problem with a query in doctrine 2. I'm using SQL Server and here is my query with doctrine :
$qb->select('MONTH(au.dateRealisation) AS month');
$qb->from($cible, 'au');
$qb->leftJoin('au.cEcart', 'ec');
$qb->leftJoin('ec.oGravite', 'grav');
$qb->where($qb->expr()->eq('grav.abrev', $qb->expr()->lit($data['gravite'])));
$qb->groupBy('month');
And when I try to execute it I've got :
An exception occurred while executing 'SELECT MONTH(a0_.dateRealisationAudit) AS sclr_0 FROM AuditCentre a1_ INNER JOIN Audit a0_ ON a1_.idAudit = a0_.idAudit LEFT JOIN Ecart e2_ ON a0_.idAudit = e2_.idAudit LEFT JOIN Gravite g3_ ON e2_.idGravite = g3_.idGravite WHERE g3_.abrevGravite = 'NC' GROUP BY sclr_0 ORDER BY sclr_0 ASC':
SQLSTATE[42S22]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Column name not valid: 'sclr_0'.
And if I use $qb->groupBy('MONTH(...)') i've got this error :
[Semantical Error] line 0, col 148 near 'MONTH(au.dateRealisation)':
Error: Cannot group by undefined identification or result variable.
MONTH function is correctly added and if I remove the groupBy it works fine.
Have you got an idea to do this ?
Thanks
This is my model function in laravel blade. In this i have two table "admins" and "candidates" using join query i join the two table. But While running my code I probably get this below mentioned
error.
if ( Input::has('recordcreator') and $request->input('recordcreator') != NULL){
$querys->join('admins','admins.adminid','=','candidates.createdby');
$querys->where('admins.firstname', 'LIKE', '%'. $request->input('recordcreator') .'%')->orWhere('admins.lastname', 'LIKE', '%'. $request->input('recordcreator') .'%')->where('admins.status','1');
}
SQLSTATE[23000]: Integrity constraint violation: 1052 Column
'firstname' in order clause is ambiguous (SQL: select * from
rsi_candidates inner join rsi_admins on rsi_admins.adminid =
rsi_candidates.createdby where rsi_admins.lastname LIKE %ic%
and rsi_admins.status = 1 or rsi_admins.firstname LIKE %ic%
group by rsi_candidates.candidateid order by firstname asc limit
20 offset 0)
As i see your laravel error you may add the table name in front of your firstname column in orderby clause
Or you may also try this solution
open your database.php file located in config directory
Config->database.php in your mysql database array add this.
'strict' => false,
I am trying to write hasMany relationship, but I am getting this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'[[licences.id = userKeys.licence_id]] = `licences`.`0` LEFT JOIN `userKeys` `use' at line 1
The SQL being executed was:
SELECT COUNT(*) FROM `activityLogsUserActivity`
LEFT JOIN `users` ON `activityLogsUserActivity`.`user_id` = `users`.`id`
LEFT JOIN `licences` ON `activityLogsUserActivity`.[[licences.id = userKeys.licence_id]] = `licences`.`0`
LEFT JOIN `userKeys` `userKeys` ON `licences`.`user_id` = `userKeys`.`user_id`
The code:
public function getKeys()
{
return $this->hasMany(UserKeys::classname(), ['user_id' => 'user_id'])
->select('licences.licenceName, userKeys.*')
->from(['userKeys' => UserKeys::tableName()])
->viaTable(Licences::tableName(), ['licences.id = userKeys.licence_id']);
}
What am I doing wrong?
['licences.id = userKeys.licence_id']
should be a key-value pair, like
['id' => 'licence_id']
and it should not be necessary to declare the table name there, have a look at the docs
http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#viaTable()-detail
The link between the junction table and the table associated with $primaryModel. The keys of the array represent the columns in the junction table, and the values represent the columns in the $primaryModel table.
i have a query as follows and it gives me a error
my query in model is
$query = $this->db->select('SELECT AV.Ad_ID, AV.Title, AV.Price, LT1.Listing')
->from('ad_vehicle')
->join('ref_listing_type', 'ad_vehicle.Listing_Type_ID = ref_listing_type.Listing_ID', 'inner')
->where('ad_vehicle.Created_By', 1)
->get();
var_dump($query);
return $query->result_array();
and my error is
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.Ad_ID, AV.Title, AV.Price, LT1.Listing FROM
(ad_vehicle) INNER JO' at line 1
SELECT `SELECT` AV.Ad_ID, `AV`.`Title`, `AV`.`Price`, `LT1`.`Listing`
FROM (`ad_vehicle`)
INNER JOIN `ref_listing_type` ON `ad_vehicle`.`Listing_Type_ID` = `ref_listing_type`.`Listing_ID`
WHERE `ad_vehicle`.`Created_By` = 1
Filename: C:\wamp\www\DoolalyJobsBackup\system\database\DB_driver.php
Line Number: 330
Change your select() to
$query = $this->db->select('AV.Ad_ID, AV.Title, AV.Price, LT1.Listing')
Using active record's select() will automatically adds a SELECT keyword you don't need to add again in your query,Also in your query you have used short aliases for the tables but you haven't assign them
$this->db->select('AV.Ad_ID, AV.Title, AV.Price, LT1.Listing')
->from('ad_vehicle AV')
->join('ref_listing_type LT1', 'AV.Listing_Type_ID = LT1.Listing_ID', 'inner')
->where('AV.Created_By', 1)
->get();