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]);
Related
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
I want To combine AND condition with OR condition in codeigniter SQL query
i am creating a where condition depending on the users input.
when a user select rent, iam passing OR condition inside WHERE(AND) like this $where['p.contract_id = 3 OR p.contract_id']=2;.
here is my code..
$where = array('p.status' => 1, 'pi.order' => 1);
if(!$fil['city'] == 0){
$where['p.city_id']=$fil['city'];
}
if(!$fil['area'] == 0){
$where['p.area_id']=$fil['area'];
}
if(!$fil['type'] == 0){
$where['p.type_id']=$fil['type'];
}
if(!$fil['bed'] == 0 && !$fil['bed']== 10){
$where['pd.bed']=$fil['bed'];
}
if($fil['bed']== 10){
$where['pd.bed >']=$fil['bed'];
}
if(!$fil['bath'] == 0 && !$fil['bath'] == 10){
$where['pd.bath']=$fil['bath'];
}
if($fil['bath'] == 10){
$where['pd.bath >']=$fil['bath'];
}
if(!is_null($fil['rent'])){
$where['p.contract_id = 3 OR p.contract_id']=2;
}
if(!is_null($fil['sale'])){
$where['p.contract_id']=1;
}
if(!$fil['price_from'] ==""){
$where['pd.price >']=$fil['price_from'];
}
//real query
$this->db->select('p.*, pd.title, pd.price, pd.bed, pd.bath, pd.size, pd.size_type, pd.full_description,
pi.name as image, pi.order, a.name as area, t.name as type_name, ct.name as contract_type, pv.views as views,
c.name as city');
$this->db->from('property p');
$this->db->join('property_detail pd', 'pd.property_id=p.property_id', 'left');
$this->db->join('property_image pi', 'pi.property_id=p.property_id', 'left');
$this->db->join('type t', 't.type_id=p.type_id', 'left');
$this->db->join('contract_type ct', 'ct.con_typ_id=p.contract_id', 'left');
$this->db->join('property_views pv', 'pv.property_id=p.property_id', 'left');
$this->db->join('area a', 'a.area_id=p.area_id', 'left');
$this->db->join('city c', 'c.city_id=p.city_id', 'left');
$this->db->where($where);
$this->db->order_by('p.property_id','desc');
$this->db->limit($config['per_page'], $this->uri->segment(3));
$query = $this->db->get();
$result = $query->result();
return $result;
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 '2' at line 12
SELECT 'p'.*,
'pd'.'title',
'pd'.'price',
'pd'.'bed',
'pd'.'bath',
'pd'.'size',
'pd'.'size_type',
'pd'.'full_description',
'pi'.'NAME' AS 'image',
'pi'.'ORDER',
'a'.'NAME' AS 'area',
't'.'NAME' AS 'type_name',
'ct'.'NAME' AS 'contract_type',
'pv'.'views' AS 'views',
'c'.'NAME' AS 'city'
FROM 'property' 'p'
LEFT JOIN 'property_detail' 'pd'
ON 'pd'.'property_id'='p'.'property_id'
LEFT JOIN 'property_image' 'pi'
ON 'pi'.'property_id'='p'.'property_id'
LEFT JOIN 'type' 't'
ON 't'.'type_id'='p'.'type_id'
LEFT JOIN 'contract_type' 'ct'
ON 'ct'.'con_typ_id'='p'.'contract_id'
LEFT JOIN 'property_views' 'pv'
ON 'pv'.'property_id'='p'.'property_id'
LEFT JOIN 'area' 'a'
ON 'a'.'area_id'='p'.'area_id'
LEFT JOIN 'city' 'c'
ON 'c'.'city_id'='p'.'city_id'
WHERE 'p'.'status' = 1
AND 'pi'.'ORDER' = 1
AND 'p'.'contract_id' = 3
OR p.contract_id 2
i Know very Well this error is Caused because if this $where['p.contract_id = 3 OR p.contract_id']=2;. Can some one help me to pass this where condition with OR statement in codeigniter. Tnx..
This might work for you. Here I've placed or_where() and where() after where condition which checks for your if condition
$this->db->distinct();
$this->db->select('p.*, pd.title, pd.price, pd.bed, pd.bath, pd.size, pd.size_type, pd.full_description,
pi.name as image, pi.order, a.name as area, t.name as type_name, ct.name as contract_type, pv.views as views,
c.name as city',false);
$this->db->from('property p');
$this->db->join('property_detail pd', 'pd.property_id=p.property_id', 'left');
$this->db->join('property_image pi', 'pi.property_id=p.property_id', 'left');
$this->db->join('type t', 't.type_id=p.type_id', 'left');
$this->db->join('contract_type ct', 'ct.con_typ_id=p.contract_id', 'left');
$this->db->join('property_views pv', 'pv.property_id=p.property_id', 'left');
$this->db->join('area a', 'a.area_id=p.area_id', 'left');
$this->db->join('city c', 'c.city_id=p.city_id', 'left');
$this->db->where($where);
if(!is_null($fil['rent'])){
$this->db->where('p.contract_id','3');
$this->db->or_where('p.contract_id','2');
}
$this->db->order_by('p.property_id','desc');
$this->db->limit($config['per_page'], $this->uri->segment(3));
$query = $this->db->get();
$result = $query->result();
return $result;
Output
SELECT `p`.*, `pd`.`title`, `pd`.`price`, `pd`.`bed`, `pd`.`bath`, `pd`.`size`, `pd`.`size_type`, `pd`.`full_description`, `pi`.`name` as image, `pi`.`order`, `a`.`name` as area, `t`.`name` as type_name, `ct`.`name` as contract_type, `pv`.`views` as views, `c`.`name` as city FROM (`property` p) LEFT JOIN `property_detail` pd ON `pd`.`property_id`=`p`.`property_id` LEFT JOIN `property_image` pi ON `pi`.`property_id`=`p`.`property_id` LEFT JOIN `type` t ON `t`.`type_id`=`p`.`type_id` LEFT JOIN `contract_type` ct ON `ct`.`con_typ_id`=`p`.`contract_id` LEFT JOIN `property_views` pv ON `pv`.`property_id`=`p`.`property_id` LEFT JOIN `area` a ON `a`.`area_id`=`p`.`area_id` LEFT JOIN `city` c ON `c`.`city_id`=`p`.`city_id` WHERE `p`.`status` = 1 AND `pi`.`order` = 1 AND `p`.`contract_id` = '3' OR `p`.`contract_id` = '2' ORDER BY `p`.`property_id` desc LIMIT 0
You can try something like that:
$this->db->where('(p.contract_id = 3 OR p.contract_id =2) AND (...) ', NULL, FALSE);
The third parameter tells ci that it should not try to protect your field names. You will have to care yourself for injection problems.
SELECT `p`.*, `pd`.`title`, `pd`.`price`, `pd`.`bed`, `pd`.`bath`, `pd`.`size`, `pd`.`size_type`, `pd`.`full_description`, `pi`.`name` as `image`, `pi`.`order`, `a`.`name` as `area`, `t`.`name` as `type_name`, `ct`.`name` as `contract_type`, `pv`.`views` as `views`, `c`.`name` as `city` FROM `property` `p` LEFT JOIN `property_detail` `pd` ON `pd`.`property_id`=`p`.`property_id` LEFT JOIN `property_image` `pi` ON `pi`.`property_id`=`p`.`property_id` LEFT JOIN `type` `t` ON `t`.`type_id`=`p`.`type_id` LEFT JOIN `contract_type` `ct` ON `ct`.`con_typ_id`=`p`.`contract_id` LEFT JOIN `property_views` `pv` ON `pv`.`property_id`=`p`.`property_id` LEFT JOIN `area` `a` ON `a`.`area_id`=`p`.`area_id` LEFT JOIN `city` `c` ON `c`.`city_id`=`p`.`city_id`
WHERE `p`.`contract_id` IN ('3', '2') AND `p`.`status` = 1 AND `pi`.`order` = 1 ;
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'
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.
I'm trying to make a mail system in Codeigniter with the PyroCms.
In my mail table, I have a "recipent" row and a "sender" row which contains the user id of the sender and recipient. To retrieve usernames from the ids, I'm trying to join the table together, but it simply returns me this error:
Error Number: 1066
Not unique table/alias: 'default_users'
SELECT `default_mailsystem`.*, `default_users`.`username` AS modtager, `default_users`.`username` as afsender
FROM (`default_mailsystem`)
LEFT JOIN `default_users` ON `default_mailsystem`.`recipent` = `default_modtager`.`id`
LEFT JOIN `default_users` ON `default_mailsystem`.`sender` = `default_afsender`.`id`
ORDER BY `id` DESC
Filename: /hsphere/local/home/brightmedia/reuseable.dk/modules/mail/models/mail_m.php
Line Number: 13
My code is as follows:
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
->join('users', 'mailsystem.recipent = modtager.id', 'left')
->join('users', 'mailsystem.sender = afsender.id', 'left');
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
The funny thing is, that if I remove the last "join" operation and leave it to only join the recipient of the mail it all works out well.
This is very simple
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
$this->db->join('users', 'mailsystem.recipent = modtager.id AND mailsystem.sender = afsender.id', 'left')
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
Have you tried forcing an alias in the join function (the "AS" operator won't work in the select clause as you have it...)?
<?php
$this->db->select('mailsystem.*, modtager.username AS modtager_name, afsender.username as afsender_name')
->join('`users` `modtager`', 'mailsystem.recipent = modtager.id', 'left')
->join('`users` `afsender`', 'mailsystem.sender = afsender.id', 'left');
$this->db->order_by('mailsystem.id', 'DESC');
return $this->db->get('mailsystem')->result();
Use alias like this -
$this->db->select('mailsystem.*, users_table_a.username AS modtager, users_table_b.username as afsender')
$this->db->join('users users_table_a', 'mailsystem.recipent = users_table_a.id', 'left');
$this->db->join('users users_table_b', 'mailsystem.sender = users_table_b.id', 'left');
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
you can use this :
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
$this->db->join('users', 'mailsystem.recipent = modtager.id AND mailsystem.sender = afsender.id', 'left')
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
*If you are using any db prefix use this *
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
$this->db->join('users', 'mailsystem.recipent = modtager.id AND '.$this->db->dbprefix('mailsystem').'.sender = '.$this->db->dbprefix('afsender').'.id', 'left')
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
Its very easy to get data from single table
$this->db->select('a.*,b.fname AS cname,c.fname as uname');
$this->db->from('tbl_menus a');
$this->db->join('tbl_admin_login b', 'b.id = a.create_by', 'left');
$this->db->join('tbl_admin_login c', 'c.id = a.update_by', 'left');
$this->db->order_by('a.id', 'desc');
return $this->db->get()->result_array();
At the time of this thread, i figured that the codeigniter call missed out the possibility to do AS inside of a join, Therefor this solved the issue:
$sql = "
SELECT default_mailsystem.*,
recipent.first_name AS modtager,
sender.first_name AS afsender
FROM default_mailsystem
LEFT JOIN default_profiles AS recipent ON recipent.id = default_mailsystem.id
LEFT JOIN default_profiles AS sender ON sender.id = default_mailsystem.id
";
return $this->db->query($sql)->result();
Your can try this
Core PHP
SELECT `custome_module`.`id`, `custome_module`.`name`, min(l1.nmark_completed) as call_waiter, min(l2.nmark_completed) as bill, min(l3.nmark_completed) as tray, min(l4.nmark_completed) as ordera
FROM `custome_module`
LEFT JOIN `restaurant_logs` as `l1` ON `custome_module`.`id` = `l1`.`nmodule_id` AND `l1`.`ntype` = 1
LEFT JOIN `restaurant_logs` as `l2` ON `custome_module`.`id` = `l2`.`nmodule_id` AND `l2`.`ntype` = 2
LEFT JOIN `restaurant_logs` as `l3` ON `custome_module`.`id` = `l3`.`nmodule_id` AND `l3`.`ntype` = 6
LEFT JOIN `restaurant_logs` as `l4` ON `custome_module`.`id` = `l4`.`nmodule_id` AND `l4`.`ntype` = 5
WHERE `custome_module`.`nbranch_id` = '142'
GROUP BY `custome_module`.`id`
and also in Codeigniter
$this->db->select('custome_module.id, custome_module.name, min(l1.nmark_completed) as call_waiter, min(l2.nmark_completed) as bill,min(l3.nmark_completed) as tray,min(l4.nmark_completed) as ordera');
$this->db->from("custome_module");
$this->db->join('restaurant_logs as l1', 'custome_module.id = l1.nmodule_id AND l1.ntype = 1', 'left');
$this->db->join('restaurant_logs as l2', 'custome_module.id = l2.nmodule_id AND l2.ntype = 2', 'left');
$this->db->join('restaurant_logs as l3', 'custome_module.id = l3.nmodule_id AND l3.ntype = 6', 'left');
$this->db->join('restaurant_logs as l4', 'custome_module.id = l4.nmodule_id AND l4.ntype = 5', 'left');
$this->db->where('custome_module.nbranch_id', $this->data['user_session']['nid']);
$this->db->get();