Want to get all services in one to many relationship
My code is
$this->db->select('*');
$this->db->from('service');
$this->db->join('user', 'user.user_email = service.user_email', 'inner');
$this->db->join('service', 'service.user_email = user.user_email', 'inner');
$query = $this->db->get();
But it gives me an error
Error Number: 1066
Not unique table/alias: 'service'
SELECT * FROM (`service`) INNER JOIN `user` ON `user`.`user_email` = `service`.`user_email` INNER JOIN `service` ON `service`.`user_email` = `user`.`user_email`
Filename: C:\xampp\htdocs\service\system\database\DB_driver.php
If I do without
$this->db->from('service');
Then it gives syntax error
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 'INNER JOIN `user` ON `user`.`user_email` = `service`.`user_email` INNER JOIN `se' at line 2
SELECT * INNER JOIN `user` ON `user`.`user_email` = `service`.`user_email` INNER JOIN `service` ON `service`.`user_email` = `user`.`user_email`
Filename: C:\xampp\htdocs\service\system\database\DB_driver.php
You are trying to join the service table to the user table and then trying to join the service table again.
This isn't something you should generally be trying to do, and when you do it this way, the rendered SQL statement contains two attempts to reference the service table. It's the second JOIN that causes the database pain and it throws the error that you're seeing.
In this instance, just use:
$this->db->select('*');
$this->db->from('service');
$this->db->join('user', 'user.user_email = service.user_email', 'inner');
$query = $this->db->get();
Since the
$this->db->join('service', 'service.user_email = user.user_email', 'inner');
is redundant here (you've already joined these tables on that field.
Related
I need help on a particular codeigniter 3.1.6 active record query that keeps returning an error.
The code itself is:
$this->db->select('ms_payments.id, ms_payments.user_id, ms_payments.memberships_user_id, ms_payments.staff_id, ms_payments.rate_amount, ms_payments.tax, ms_payments.coupon, ms_payments.total, ms_payments.currency, ms_payments.pp, ms_payments.date,
u1.first_name AS user_name, u1.last_name AS user_last_name, u2.first_name AS staff_name, u2.last_name AS staff_last_name,
ms_gateways.displayname AS pp, ms_memberships_users.id, ms_memberships.title AS membership')
->from('ms_payments')
->join('auth_users as u1', 'u1.id = ms_payments.user_id')
->join('auth_users as u2', 'u2.id = ms_payments.staff_id')
->join($this->gTable, 'ms_gateways.id = ms_payments.pp')
->join($this->muTable, 'ms_memberships_users.id = ms_payments.memberships_user_id')
->join($this->mTable, 'ms_memberships.id = ms_memberships_users.membership_id')
->where('ms_payments.box_id =', $this->box_id);
$result = $this->db->get()->result();
The issue is related to the first two joins, I suspect is because the table name aliases.
The SQL query generated by $this->db->get_compiled_select() query works perfectly:
SELECT `ms_payments`.`id`, `ms_payments`.`user_id`, `ms_payments`.`memberships_user_id`, `ms_payments`.`staff_id`, `ms_payments`.`rate_amount`, `ms_payments`.`tax`, `ms_payments`.`coupon`, `ms_payments`.`total`, `ms_payments`.`currency`, `ms_payments`.`pp`, `ms_payments`.`date`,
`u1`.`first_name` AS `user_name`, `u1`.`last_name` AS `user_last_name`, `u2`.`first_name` AS `staff_name`, `u2`.`last_name` AS `staff_last_name`, `ms_gateways`.`displayname` AS `pp`,
`ms_memberships_users`.`id`, `ms_memberships`.`title` AS `membership`
FROM `ms_payments`
JOIN `auth_users` as `u1` ON `u1`.`id` = `ms_payments`.`user_id`
JOIN `auth_users` as `u2` ON `u2`.`id` = `ms_payments`.`staff_id`
JOIN `ms_gateways` ON `ms_gateways`.`id` = `ms_payments`.`pp`
JOIN `ms_memberships_users` ON `ms_memberships_users`.`id` = `ms_payments`.`memberships_user_id`
JOIN `ms_memberships` ON `ms_memberships`.`id` = `ms_memberships_users`.`membership_id`
WHERE `ms_payments`.`box_id` = '1'
and yet I still get this codeigniter error, any clues why?
Blockquote An uncaught Exception was encountered
Blockquote Type: Error
Blockquote Message: Call to a member function result() on boolean
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 two tables project and workload. I want show user in some project that added in Workload, and anothers member in this project (I called Team Member of that Project), too.
My idea is join 2 tables project and workload with user_id condition to take projects of user have user_id, then, from that will join with workload table again to take data from project of user have that user_id and user_id of team member will have that projects.
That my code in WorkloadSearch.php
public function searchWorkloadofUser($params) {
$user_id = Yii::$app->user->id;
$query = Workload::find()
->select(['workload.project_id', 'workload.commit_time', 'project.project_name', 'workload.from_date', 'workload.to_date', 'workload.workload_type', 'workload.comment'])
->join('INNER JOIN', 'project', 'workload.project_id=project.id')
->where('workload.user_id = '.$user_id)->orderBy('project.project_name ASC')->distinct();
$query->join('INNER JOIN','workload', 'project.id = workload.project_id')->distinct();
}
I don't understand why appeared error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'workload'
The SQL being executed was: SELECT COUNT(*) FROM (SELECT DISTINCT `workload`.`project_id`, `workload`.`commit_time`, `project`.`project_name`, `workload`.`from_date`, `workload`.`to_date`, `workload`.`workload_type`, `workload`.`comment` FROM `workload` INNER JOIN `project` ON workload.project_id=project.id INNER JOIN `workload` ON project.id = workload.project_id WHERE workload.user_id = 20) `c`
like the others said: You need an alias. Your error messages tells you:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'workload'
This is beacause you joined your table worload to your workload table:
$query = Workload::find() //your `workload` table
->select([
....
])
->join('INNER JOIN', 'project p'], 'workload.project_id=p.id')
->where('workload.user_id = '.$user_id)
->orderBy('p.project_name ASC')
->distinct();
$query->join('INNER JOIN','workload', 'project.id = workload.project_id') //join the `workload` table to the `workload` table
->distinct();
When you want to join a table to itself, you have to define an alias at least for the joining table.
I would recommend you to quote the tablenames and columns whis is described in the Yii2 guide here and do not concat strings but bind params like it's described here and here. This code should work for you (not tested):
$query = Workload::find() //your `workload` table
->select([
'{{workload}}.[[project_id]]', //quoting tablenames and columns
'{{%workload}}.[[commit_time]]', //add '%' when you're using table prefix
...
])
->join('INNER JOIN', 'project p'], 'workload.project_id=p.id')
->where('workload.user_id' = :user_id, [':user_id' => $user_id]) //You should bind params when use string format
//or use hash format
//->where(['workload.user_id' => $user_id])
...
$query->join('INNER JOIN','workload w2', 'project.id = w2.project_id') //Use the alias `w2` to join the `workload` table to the `workload` table
->distinct();
You need to give the user table an alias the second time you join to it
e.g.
SELECT workload.project_id, workload.commit_time, p.project_name, ...
FROM workload
LEFT JOIN project p ON p.id = workload.project_id
WHERE workload.user_id = '1'
Your solution should be (untested):
public function searchWorkloadofUser($params)
{
$user_id = Yii::$app->user->id;
$query = Workload::find()
->select(['workload.project_id', 'workload.commit_time', 'p.project_name', 'workload.from_date', 'workload.to_date', 'workload.workload_type', 'workload.comment'])
->join('INNER JOIN', 'project p'], 'workload.project_id=p.id')
->where('workload.user_id = '.$user_id)->orderBy('p.project_name ASC')->distinct();
...
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();
I am trying to write a query in yii. I have the following which works
$criteria = new CDbCriteria;
$criteria->condition = "($column = :id)";
$criteria->params = array(":id" => $id );
$rows = Jobs::model()->with('pROJ')->findAll($criteria);
This returns the model of Jobs in array. I need to write the following query in yii to return a model
SELECT jobs.JOBNO, jobs.STATUS, projects.ORDERNO, jobs.PROJID, jobs.NAME, jobs.SEQ, jobs.PCENTDONE, jobs.EARNED, jobs.VALUE, jobs.DATEIN, jobs.DATEDONE, jobs.DATEDUE, jobs.SENTBACK, jobs.ORIGTAPES, jobs.COMMENTS, projects.CATEGORY, orders.BIDNO
FROM (jobs INNER JOIN projects ON jobs.PROJID = projects.PROJID) INNER JOIN orders ON projects.ORDERNO = orders.ORDERNO
where jobs.projid = 3002001
ORDER BY jobs.JOBNO, jobs.PROJID
I have tried the following but it does not work
$rows = Yii::app()->db->createCommand()
->select('jobs.*, projects.ORDERNO, projects.CATEGORY, orders.BIDNO')
->from('jobs, projects, orders')
->join('projects p','jobs.PROJID = p.PROJID')
->join('orders o', 'p.ORDERNO = o.ORDERNO')
->where('jobs.projid=:id', array(':id'=>$id))
->queryRow();
I get the following error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jobs.PROJID' in 'on clause'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`ORDERNO`, `projects`.`CATEGORY`, `orders`.`BIDNO`
FROM `jobs`, `projects`, `orders`
JOIN `projects` `p` ON jobs.PROJID=p.PROJID
JOIN `orders` `o` ON p.ORDERNO=o.ORDERNO
WHERE jobs.projid=:id
I have updated to
$rows = Yii::app()->db->createCommand()
->select('jobs.*, projects.orderno, projects.category, orders.bidno')
->from('jobs')
->join('projects p','jobs.projid = p.projid')
->join('orders o', 'p.orderno = o.orderno')
->where('jobs.projid=:id', array(':id'=>$id))
->queryRow();
but i still get the error. All columns in mysql are CAPS
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects.orderno' in 'field list'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`orderno`, `projects`.`category`, `orders`.`bidno`
FROM `jobs`
JOIN `projects` `p` ON jobs.projid = p.projid
JOIN `orders` `o` ON p.orderno = o.orderno
WHERE jobs.projid=:id
As #DCoder said: your select should now read select('jobs.*, p.orderno, p.category, o.bidno'). For consistency you should also alias jobs as follows
$rows = Yii::app()->db->createCommand()
->select('j.*, p.orderno, p.category, o.bidno')
->from('jobs j')
->join('projects p','j.projid = p.projid')
->join('orders o', 'p.orderno = o.orderno')
->where('j.projid=:id', array(':id'=>$id))
->order('j.jobno,j.projid')
->queryRow();
I think you should remove projects, orders from ->from('jobs, projects, orders') and may be you should lower the case of jobs.PROJID as your error message says that it couldn't find the column.