yii multiple inner joins - php

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.

Related

Error Number: 1054 Unknown column '2021-08-23' in 'on clause'

I have join many tables in this query via LEFT JOIN and i want to apply condition on table but php is giving me following error
Error Number: 1054 Unknown column '2021-08-23' in 'on clause'
SELECT i.isn_Add_Date, s.scentre_Name, i.isn_Job_No,cp.cpart_Part_Id,
cp.cpart_Part,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount,
p.product_Name,m.model_Name,c.complaint_Job_No,c.complaint_Add_Date,
c.complaint_Under_Warranty,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount
FROM complaints as c LEFT JOIN complaint_parts as cp ON c.complaint_Id
= cp.cpart_Complaint_Id LEFT JOIN scentres as s ON s.scentre_Id = c.complaint_Scentre_Id LEFT JOIN products as p ON p.product_Id =
c.complaint_Product_Id LEFT JOIN models as m ON m.model_Id =
c.complaint_Model_Id LEFT JOIN isn as i ON i.isn_Complain_Number =
c.complaint_Job_No AND i.isn_Add_Date BETWEEN '2021-08-23' AND
'2021-08-26' WHERE c.complaint_Scentre_Id = '1' AND c.complaint_Status
= 'Delivered' AND c.complaint_Trash = 0
here is my php code
$this->db->select('i.isn_Add_Date,s.scentre_Name,i.isn_Job_No,cp.cpart_Part_Id,cp.cpart_Part,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount,p.product_Name,m.model_Name,c.complaint_Job_No,c.complaint_Add_Date,c.complaint_Under_Warranty,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount');
$this->db->join('complaint_parts as cp', 'c.complaint_Id = cp.cpart_Complaint_Id ', 'left');
$this->db->join('scentres as s', 's.scentre_Id = c.complaint_Scentre_Id', 'left');
$this->db->join('products as p', 'p.product_Id = c.complaint_Product_Id', 'left');
$this->db->join('models as m', 'm.model_Id = c.complaint_Model_Id', 'left');
$this->db->JOIN('isn as i', 'i.isn_Complain_Number = c.complaint_Job_No AND i.isn_Add_Date BETWEEN '.str_replace('/', '-', $fromDate).' AND '.str_replace('/', '-', $toDate).'', 'LEFT');
$this->db->where("c.complaint_Scentre_Id", $serviceCenterId[0]);
$this->db->where("c.complaint_Status",'Delivered');
$this->db->where("c.complaint_Trash", 0);
$query = $this->db->get("complaints as c");
change
`2021-08-23` AND `2021-08-26`
to
'2021-08-23' AND '2021-08-26'
as the ` character implies columns and not strings
the sql is ok
maybe the orm can not handle complex join condition
you can move the join condition 【AND i.isn_Add_Date BETWEEN '2021-08-23' AND '2021-08-26'】 to where condition
just like $this->db->where("i.isn_Add_Date", BETWEEN ,['2021-08-23','2021-08-26]);

Doctrine: From SQL to DQL

I've been trying to do a regular query with an inner join in Doctrine's DQL but with little success:
SQL:
SELECT
r.rtsRate
FROM tmc_rates_tbl_rts as r
INNER JOIN tmc_business_rel_buz as b ON (b.buzPcsID = r.rtsBuzID AND b.buzCmrID = r.rtsCmrID)
WHERE b.buzID = :business
AND r.rtsProID = :user;
DQL:
$qb = $this->rateRepo->createQueryBuilder('r')
->select('r.rate')
->innerJoin('r.business', 'business')
->where('r.business = :bid')
->andWhere('r.user = :user')
->setParameter('bid', $time->getBusiness())
->setParameter('user', $this->user)
->getQuery()
->execute();
Doctrine cames out with a double join out of the blue:
An exception occurred while executing 'SELECT t0_.rtsRate AS rtsRate_0 FROM tmc_rates_tbl_rts t0_ INNER JOIN rate_business r2_ ON t0_.id = r2_.rate_id INNER JOIN tmc_business_rel_buz t1_ ON t1_.id = r2_.business_id AND (t1_.buzStatus = 1) WHERE t1_.buzID = ? AND t0_.rtsProID = ?' with params [9, 5]:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tm_salesn.rate_business' doesn't exist
500 Internal Server Error - TableNotFoundException
I very much appreciate any help

How to write this query in yii?

SELECT business.id,business.business_name,address.city FROM business INNER JOIN address ON business.id=address.business_id WHERE business.business_name like '%monal%' and address.city='islamabad'
Where monal and islamabad would be a value coming from a form.
when i replace islamabad with a variable it gives me error.
My query in yii.
$user = Yii::app()->db->createCommand()
->select('business.id,business_name,business.image,business.business_description,address.city')
->from('business')
->join('address', 'business.id=address.business_id')
//->where(array('like', 'business.business_name', '%'.$name.'%'))
->where(array('and', 'address.city=$city', array('like', 'business.business_name', '%'.$name.'%')))
->queryALL();
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Islamabad' in 'where clause'. The SQL statement executed was: SELECT `business`.`id`, `business_name`, `business`.`image`, `business`.`business_description`, `address`.`city`
FROM `business`
JOIN `address` ON business.id=address.business_id
WHERE (address.city=Islamabad) AND (`business`.`business_name` LIKE '%nan%')
Try this as your query instead
$results = Yii::app()->db->createCommand()
->select('b.id, b.business_name, a.city')
->from('business b')
->join('address a', 'b.id = a.business_id')
->where('b.business_name LIKE :businessName AND a.city = :city', array(
':businessName' => '%' . $businessNameVariable . '%',
':city' => $cityVariable,
))
->queryAll();

Laravel Query Builder join doesn't affect query

I'm trying to build in search functionality in my application (Laravel 5.1), but my join doesn't seem to do anything to the resulting query. What am I doing wrong?
Code:
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
$query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
Resulting query:
select count(*) as aggregate from invoice_headers where customer_code_id = 1 and (invoice_types.name <> 380)
Resulting response:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invoice_types.name' in 'where clause'
This is the query I was hoping to see:
select count(*) as aggregate
from invoice_headers
inner join invoice_types
on invoice_headers.invoice_type_id = invoice_types.id
where customer_code_id = 1
and (invoice_types.name <> 380)
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
you need to store the query in a variable.
$query = $query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
You need to add JOIN with invoice_types table if you want to filter on invoice_types.name.

Yii CDbCriteria adding unwanted alias

I need to run following statements by CDbCriteria in Yii :
SELECT `tbl_products`.`id` FROM `tbl_products`
INNER JOIN `tbl_producttags`
ON `tbl_products`.`id` = `tbl_producttags`.`product_id`
INNER JOIN `tbl_tags`
ON `tbl_producttags`.`tag_id` = `tbl_tags`.`id`
What I've tried so far :
$criteria = new CDbCriteria();
$criteria->select= '`tbl_products`.`id`';
$criteria->join ='INNER JOIN `tbl_producttags` ON `tbl_products`.`id` = `tbl_producttags`.`product_id`'
. ' INNER JOIN `tbl_tags` ON `tbl_producttags`.`tag_id` = `tbl_tags`.`id`';
$products = Products::model()->findAll($criteria);
But it get me following error :
SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'tbl_products.id' in 'field list'. The SQL statement executed was:
SELECT tbl_products.id FROM tbl_products t INNER JOIN
tbl_producttags ON tbl_products.id =
tbl_producttags.product_id INNER JOIN tbl_tags ON
tbl_producttags.tag_id = tbl_tags.id
The problem is :
It's because of that CDbCriteria added unwanted alias name t after tbl_products
How can I fix it?
Use this
$criteria = new CDbCriteria();
$criteria->select= '`yourAlias`.`id`';
$criteria->alias="yourAlias";
$criteria->join ='INNER JOIN `tbl_producttags` ON `tbl_products`.`id` = `tbl_producttags`.`product_id`'
. ' INNER JOIN `tbl_tags` ON `tbl_producttags`.`tag_id` = `tbl_tags`.`id`';
$products = Products::model()->findAll($criteria);
the alias name of the table. If not set, it means the alias is 't'

Categories