I currently have 2 tables in which I need to get data from.
My users table looks like:
ID | first_name | last_name | username
My data table looks like:
ID | filename | doctorname | viewed
What I need to do match the first name and last name from the users table to the doctor name in the data table and the return the "filename" and "viewed" rows from this.
I can't seem to get the query right for it though. I'm using the active record class with code igniter. Ive also tried to have a separate function that concats the first and last name and returns it but I can't seem to pass that variable to the join section of the query.
$avail = '1';
$this->db->select('*');
$this->db->from("users a");
$this->db->where("username",$username);
$this->db->where('CONCAT(first_name, '.', last_name) AS doctorname', FALSE);
$this->db->join('data b', "doctor name = b.doctorName", "left");
$result = $this->db->get();
// print_r($this->db->last_query());
return $result->result();
Anyone know whats going wrong?
Thanks
Try like this
$avail = '1';
$this->db->select('a.*,b.filename,b.viewed');
$this->db->from("users as a");
$this->db->join('data as b', "CONCAT(a.first_name, '.', a.last_name) = b.doctorName", "left");
$this->db->where("a.username",$username);
$result = $this->db->get();
// print_r($this->db->last_query());
return $result->result();
Related
Scenario is this: Need to get all sites which don't have licensee ID = XXX.
$data = DB::select("SELECT * FROM sites AS s
WHERE status = 'Existing Site' AND
$sharerId NOT IN (SELECT organisation_id
FROM licensees AS l
WHERE site_id = s.id)
order by siteName;") ;
This code does that fine.
But I get an array, which I can't filter on later using ::where(..), just in case the user wants to search for site name YYY. I've got a generic model method that handles all types of searches and sorts, I don't want to duplicate the code just for this.
$sharerId has the ID to exclude from the sites.
$data = $this->select($select)
->where('status', 'Existing Site')
->whereNotIn($sharerId,
Licensee::select('organisation_id')
->where('site_id', DB::raw('sites.id'))
)->get() ;
Tried this, but I really didn't expect it to work, and it didn't :-P
My crazy idea did work in the end:
$data = $this->select($select)
->where('status', 'Existing Site')
->whereNotIn(DB::raw($sharerId),
Licensee::select('organisation_id')
->where('site_id', DB::raw('sites.id'))) ;
All I needed was DB::raw($sharerId)
Basicly i have two tables photos and users. I wanna join tables and Update colums image_max and image_min. I get error unknown colum username. In which way i can join two tabels and get data from both. My sintax is:
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
And I get error
Unknown column username in where clause
UPDATE `photos` SET `image_max` = '', `image_min` = '' WHERE `username` = 'wwww'
apparently you need a letter on the table should say "users.username", check that.
Greetings.
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('users.username',$username);
$this->db->update('photos',$data);
You don't need to use "select and from" before upload fields, just update in this way
$data = array('image_max'=> 4, 'image_min' => 1);
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
In codeigniter i am creating a general function for some data insertion
for it i am keeping some norms
say i have two tables one being dt_category and the other being dt_product
here's the dt_category struture
|-------------------|---------------------|--------------------|------------------|
| category_id | category_name | category_slug | timestamp |
|-------------------|---------------------|--------------------|------------------|
and here's the dt_product structure
|-------------------|---------------------|--------------------|------------------|
| product_id | product_name | product_slug | timestamp |
|-------------------|---------------------|--------------------|------------------|
as u can see, both the tables fields just change by the field-name prefix, i mean in dt_category its category_id
and in dt_product its product_id and so on
now while calling the function to insert category data i use this method
$category_name=$this->input->post('category_name');
$insert_array=array('category_name'=>$category_name,
'timestamp'=>time());
$data['msg']=$this->erp_model->data_insert('dt_category',$insert_array);
and for product data
just this method
$product_name=$this->input->post('product_name');
$insert_array=array('product_name'=>$product_name,
'timestamp'=>time());
$data['msg']=$this->erp_model->data_insert('dt_product',$insert_array);
the function that deals with the insertion is this one
function data_insert($table,$data)
{
$this->db->insert($table,$data);
$timestamp=$data['timestamp'];
$table_nm=explode('_',$table); //spliting the table name in format dt_table_name,
$table_slug=$table_nm[1]."_"."slug";//if its dt_product table_nm[1]="product",
//if category,table_nm[1]="category"
//thus $table_slug="product_slug" or "category_slug"
$query = $this->db->select('*')
->from($table)
->where('timestamp', $timestamp)
->get();
foreach($query->result() as $r_q)
{
$id=$r_q->$table_nm[1].'_id';//i want $id value of product_id since $table_nm[1]="product"
$name=$r_q->$table_nm[1].'_name';//i want $name value of product_name since $table_nm[1]="product"
}
echo $id."-".$name;
}
in the aboce function based on the table_name
i want to fetch the value of field name
$id=$r_q->$table_nm[1].'_id'; should resemble $id=$r_q->product_id; if the table is dt_product
i know i am doing the wrong way, because ->product_id is a resultanat object while i am trying to expressing as a string,
is there a way of how i can achieve the thing,
i mean fetching the field name based on the table name;
Ok, i solved my problem, and here's the answer
i was trying the concatenation at the time of retrieval,
but now i did it first and then assigned the field
$tab_id=$table_nm[1].'_id';
$tab_name=$table_nm[1].'_name';
here's the resolved code
function data_insert($table,$data)
{
$this->db->insert($table,$data);
$timestamp=$data['timestamp'];
$table_nm=explode('_',$table);
$table_slug=$table_nm[1]."_"."slug";
echo $table_slug;
$query = $this->db->select('*')
->from($table)
->where('timestamp', $timestamp)
->get();
$tab_id=$table_nm[1].'_id';
$tab_name=$table_nm[1].'_name';
foreach($query->result() as $r_q)
{
$id=$r_q->$tab_id;
$name=$r_q->$tab_name;
}
echo $id."-".$name;
}
I wanted to know how to check whether there is a value present in a table (managers) and then add a 'yes' or 'no' string depending on if there is a value in that table or not.
$this->db->select('employees.first_name, employees.last_name, departments.department_name, departments.department_numb, titles.title');
$this->db->from('employees');
$this->db->where('first_name', $firstname);
$this->db->where('last_name', $lastname);
$this->db->join('department_manager', 'department_manager.emp_numb = employees.emp_numb', 'inner');
$this->db->join('departments', 'departments.department_numb = department_manager.department_numb', 'inner');
$this->db->join('titles', 'titles.emp_numb = employees.emp_numb', 'inner');
$this->db->where('department_name', $dept);
$this->db->where('title', $jobtitle);
$result = $this->db->get();
$data = array();
foreach($result->result() as $row)
{
$entry = array();
$entry['firstname'] = $row->first_name;
$entry['lastname'] = $row->last_name;
$entry['jobtitle'] = $row->title;
$entry['dept'] = $row->department_name;
$entry['deptid'] = $row->department_number;
//$entry['ismanager'] =
$data[] = $entry;
}
return $data;
I want to check whether an employee is present in the table 'department_manager' which is joined by an employees number. So if that employee number is not present in the table 'department_manager' then I want to insert in the array index $entry[ismanager'] a string which says 'no', and if the employee number is present in the table 'department_manager' then I want $entry['ismanager'] to hold the string 'yes'.
But I'm confused as to how to check that the employee is present or not in that table. Do I do it in the active record query or in the foreach loop? And if it is done in the foreach loop then how do I make that comparison as the query is completed?
Why have a field that is basically a calculated value? That's like having fields for quantity per box, quantity of boxes then saving the total items to a third field. Never save to the database something you can gain access to via a quick query. In your query above it's as simple as changing the dept manager join to a left join, including the dept manager id and saying if that field is blank in a record the person is not a manager. Using a LEFT join will return all records whether they have an entry in the management table or not.
Add: department_manager.emp_numb to the select.
The Join:
$this->db->join('department_manager', 'department_manager.emp_numb
= employees.emp_numb', 'left');
Then in the foreach:
if(!$row->department_manager.emp_numb)
{
this person is not a manager;
}
If you feel you really must have that extra field then you can still populate it with the method above.
If you are using MySQL, I think you are looking for:
IFNULL(expr1,expr2)
where:
expr1 == null condition (in your case NULL)
expr2 == replacement value
Source: Control Flow Functions
How can I select rows from two or more tables?
I'm setting default fields for a form, and I need values from two tables...
My current code reads:
$this->CI->db->select('*');
$this->CI->db->from('user_profiles');
$this->CI->db->where('user_id' , $id);
$user = $this->CI->db->get();
$user = $user->row_array();
$this->CI->validation->set_default_value($user);
The example in the User Guide should explain this:
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id
See the whole thing under Active Record page in the User Guide.
Just add the other table to the "->from()" method. Something like:
$this->db->select('t1.field, t2.field2')
->from('table1 AS t1, table2 AS t2')
->where('t1.id = t2.table1_id')
->where('t1.user_id', $user_id);
I think the question was not so much about joins as how to display values from two different tables - the User Guide doesn't seem to explain this.
Here's my take:
$this->db->select('u.*, c.company, r.description');
$this->db->from('users u, company c, roles r');
$this->db->where('c.id = u.id_company');
$this->db->where('r.permissions = u.permissions');
$query = $this->db->get();
I think the syntax is incorrect.
You need to select one record. I have two tables, and I have an id from one table transfer by parameter, and the relation of both tables.
Try this
$this->db->select('*')
->from('student')
->where('student.roll_no',$id)
->join('student_details','student_details.roll_no = student.roll_no')
->join('course_details','course_details.roll_no = student.roll_no');
$query = $this->db->get();
return $query->row_array();
$SqlInfo="select a.name, b.data fromtable1 a, table2 b where a.id=b.a_id";
$query = $this->db->query($SqlInfo);
try this way, you can add a third table named as c and add an 'and' command to the sql command.
// Select From Table 1 All Fields, and From Table 2 one Field or more ....
$this->db->select('table1.*, table2.name');
$this->db->from('table1, table2');
$this->db->where('table2.category_id = table1.id');
$this->db->where('table2.lang_id',$id); // your where with variable
$query = $this->db->get();
return $query->result();