from this data how can i fetch all users where user_id in tbl_attendance not having current date with application_status 3 and 4
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
my code is like this
$date=date('Y-m-d');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" )');
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
want to combine those two lines and produce the output like if current date is present in 3 or 4 please help me
You can try like this.To combine two conditions
$this->db->where("tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='$date' AND tbl_attendance.attendance_status IN(3,4))");
seeing you're using query builder...
$this->db->group_start()
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
$this->db->where_not_in('tbl_users.user_id', 'SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in = '. $date, false)
$this->db->group_end()
should work
Related
Here am having three tables namely tbl_users,tbl_account_details,tbl_attendance.table tbl_users looks like this
user_id username role_id activated banned
1 admin 1 1 0
36 siraj 3 1 0
37 faizal 3 1 0
38 nesru 3 1 0
40 jaseer 3 1 0
42 maltu 3 1 0
43 shahul 3 1 0
table tbl_account_details looks like this
account_details_id user_id fullname designations_id
1 1 Administrator 1
33 36 siraj 2
34 37 faizal 3
35 38 nesru 3
37 40 jaseer 4
39 42 maltu 4
40 43 shahul 2
my tbl_attendance table looks like this
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
i want to get all users where users is not in onleave and onoff on the current day in the tbl_attendance.the attendance status for leave is 3 and for off is 4.
in order to get i wrote a query which looks like this
public function get_manager_cordinator_users($date)
{
$this->db->select('tbl_account_details.*', FALSE);
$this->db->select('tbl_users.*');
$this->db->join('tbl_account_details', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
$this->db->where(array('designations_id' => 2));
$this->db->or_where(array('designations_id' => 3));
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='.$date.' && attendance_status=3 OR attendance_status=4 )');
$query_result = $this->db->get('tbl_users');
$result = $query_result->result();
return $result;
}
my controller looks like this
$date=date('Y-m-d');
$data['assign_user'] = $this->Project_model->get_manager_cordinator_users($date);
please help me to solve...
I've created query which will return the data of users with designation_id 2 and 3, also which don't have attendance_status 3 and 4
Your controller code is fine -
$date = date('Y-m-d');
$data['assign_user'] = $this->Project_model->get_manager_cordinator_users($date);
Just change your model code with below one -
public function get_manager_cordinator_users($date)
{
$this->db->select( 'tbl_users.user_id, tbl_users.username,tbl_account_details.designations_id, tbl_attendance.date_in, tbl_attendance.attendance_status' );
$this->db->from( 'tbl_users' );
$this->db->join( 'tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id' );
$this->db->join( 'tbl_attendance', 'tbl_attendance.user_id = tbl_users.user_id' );
$this->db->where( 'tbl_attendance.date_in', $date );
$this->db->where('tbl_users.banned', 0);
$this->db->where('tbl_users.activated', 1);
$this->db->where_in( 'tbl_account_details.designations_id', array( '2', '3' ) );
$this->db->where_not_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
return $this->db->get()->result();
}
It will provide you required user data.
now i got it
public function get_manager_cordinator_users()
{
$date=date('Y-m-d');
$this->db->select('tbl_users.*');
$this->db->join('tbl_account_details', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" AND tbl_attendance.attendance_status IN (3,4) ) ');
$this->db->where_in( 'tbl_account_details.designations_id', array( '2','3' ) );
$this->db->where('tbl_users.banned', 0);
$this->db->where('tbl_users.activated', 1);
$query_result = $this->db->get('tbl_users');
$result = $query_result->result();
return $result;
}
use this
$this->db->select('tbl_account_details.*', FALSE);
$this->db->select('tbl_users.*');
$this->db->join('tbl_users', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
$this->db->where('(designations_id=2 or designations_id=3 )');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='.$date.' && attendance_status=3 OR attendance_status=4 )');
$query_result = $this->db->get('tbl_users');
$result = $query_result->result();
return $result;
$this->db->select('*)->from('tbl_users')->where('tbl_attendance.attendance_status <= 2')->where('tbl_attendance.data_in =',date('Y-m-d'))->join('tbl_attendance','tbl_attendance.user_id = 'tbl_users.user_id ','left')->get()->result();
I have a ptoblem with sorting $_post data in codeigniter.
Controller:
public function filter(){
...
$this->model->method($this->request->post);
...
}
Model:
$keys = $_POST-data
public function($keys) {
$this->db->select('*');
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't2.id = t1.id', 'left');
if(!empty($keys['value1']){
$this->db->where('t1.column', $keys['value1']);
}
if(!empty($keys['value2']){
$this->db->where('t1.column', $keys['value2']);
}
if($keys['value3'] || $keys['value4'] || $keys['value5']){
$array = array($keys['value3'], $keys['value4'], $keys['value5']);
$this->db->where_in('t2.column', $array);
}
$query = $this->db->get()->result();
return $query;
}
table1
id value1 valu2
1 23 24
2 12 15
table2
t1_id value (this contain value of value3, value4, value5)
2 7
2 19
2 13
2 3
1 7
1 13
1 3
It works, but if set a value3..value5, then selects all values, not a priority table1 value, I need to to do value1 and value2 it has been a priority before value3..value5
Sry for my bad english, tnx!
For example:
products_table
product_id value1 value2
1 13 9
2 13 9
3 14 10
product_to_atributes
product_id atribute_id
1 2
1 3
1 4
2 3
2 4
product_atributes
atribute_id atribute_name
2 atr_name
3 atr_name2
4 atr_name3
I need select list of product, where value1 = '13' and value2 = '9' and product_to_atributes.atribute_id IN(2,4)
you can use this:
$this->db->or_where_in();
your code should be:
if($keys['value3'] || $keys['value4'] || $keys['value5']){
$array = array($keys['value3'], $keys['value4'], $keys['value5']);
$this->db->or_where_in('t2.column', $array);
}
I have a table ipd_charges with columns
table - ipd_charges
id doctor room_category charges_cash charges_cashless
1 1 1 200 300
2 1 2 300 400
table - patient_admission
id patient_name tpa_name(if not null, equivalent to charges_cashless)
1 1 Null
2 2 1
table daily_ward_entry
id patient_name room_name doctor_name charges ( from ipd charges)
1 1 1 1 200
2 2 2 1 400
I am trying to use this query which is failing:
$model = \app\models\IpdCharges::find()
->where(['doctor'=>$id])
->andwhere(['room_category'=>$this->room_name])->one();
Thanks. Please tell me if any more info is needed.
A help will be greatly appreciated.
Please check below code :
$ipdCharges = new IpdCharges();
$ipdCharges->findOne(['doctor' => $id, 'room_category' => $this->room_name]);
Hi i have a table name messages i want to get last message from chat conversation between two users.
Table -
id from_id to_id text created
========================================
1 1 2 hi 2014-12-01 10:30:10
2 2 1 Hello 2014-12-01 10:30:20
3 3 1 Hi chinu 2014-12-01 10:32:02
4 3 1 hw r u? 2014-12-01 10:32:22
5 2 1 h r u? 2014-12-01 10:33:01
Please check my query =
//$user_id=$this->Session->read('Auth.User.id');
$user_id=1;
$fields=array('DISTINCT(Message.from_id), Message.id, Message.text, Message.created');
$this->Paginator->settings = array(
'conditions'=>array('Message.to_id'=>$user_id),
'limit' => 30,
'order' => array(
'Message.created' => 'DESC',
'Message.id' => 'DESC'
),
'fields'=>$fields,
'group'=>array('Message.from_id')
);
$inbox = $this->Paginator->paginate('Message');
pr($inbox);
$this->set('inbox', $inbox);
I want to get last message conversation between 2 and 3 id. The output looks like -
id from_id to_id text created
========================================
5 2 1 h r u? 2014-12-01 10:33:01
4 3 1 hw r u? 2014-12-01 10:32:22
Please help me. If you don't know the cakephp please post core php MySQL query here.
Thanks
chatfun
Please check this query -
SELECT * FROM (SELECT DISTINCT * FROM messages WHERE (from_id=1 OR to_id=1) ORDER BY created DESC) as m GROUP BY `from_id`
I suppose this query would work (although I don't understand why 'to_id' = 1 in your expected output, maybe I didn't get your purpose right)
SELECT 'id', 'from_id', 'to_id', 'text', 'created'
FROM Message
WHERE ('from_id' = 2 AND 'to_id' = 3) OR ('from_id' = 3 AND 'to_id' = 2)
ORDER BY created DESC
LIMIT 1
This how the query looks like to achieve what you are looking at , i.e. getting the last conversation from each from_id
select t1.* from Message
t1 left join Message t2
on t1.from_id = t2.from_id
and t1.id<t2.id where t2.id is null;
You have a primary key id auto_incremented so it could be used in the comparison instead of date.
Or
select t1.* from Message t1
where not exists (
select 1 from Message t2 where t1.from_id = t2.from_id and t1.id < t2.id
);
I have the folowing query that shows my products.
SELECT DISTINCT productsmap.id, attributes_product.waarde,
attributes_product.att_id, productsmap.name, productsmap.category
AS categoryId, brand.naam AS brand, productsmap.sku, categorie.name AS
category, productsmap.price, productsmap.shops
FROM productsmap
INNER JOIN categorie ON productsmap.category = categorie.id
INNER JOIN brand ON productsmap.brand = brand.id
INNER JOIN attributes_product ON productsmap.id = attributes_product.pmapid
WHERE productsmap.category
IN ( 2, 3, 4, 5, 6, 7, 8 )
AND productsmap.shops >0
AND (
productsmap.price >= 3.94
AND productsmap.price <= 204.99
)
AND productsmap.online =1
LIMIT 0 , 30
It gives the folowing results I cropped a bit:
(id) (waarde) (att_id) (name) (categoryId) (brand) (sku)
2 109 1 Name 4 Merk 70000
2 2013 2 Name etc etc etc
2 123 3 Name etc etc etc
3 103 1 Name2 etc etc
3 2012 2 Name2
3 123 3 Name2
4 110 1 3name
4 2013 2 3name
4 102 3 3name
Whit multiple times the same id and name only diffrent att_id and waarde. But i need to add to my query (attributes_product.att_id = 1 and waarde = 109) and (attributes_product.att_id = 2 and waarde = 2013)
But this is not possible because it are diffrent results. How can I solve this issue? Or on what can i search to solve this issue?
It attributes.product are att_id and waarde