I'm new in PHP and CodeIgniter. I have a problem.
Here is what my database looks like:
Table 1 :
---------------------------------------------
| id_table1 | data_table1_1 | data_table1_2 |
---------------------------------------------
Table 2 :
---------------------------------------------------------
| id_table2 | id_table1 | data_table2_1 | data_table2_2 |
---------------------------------------------------------
Table 3 :
---------------------------------------------------------
| id_table3 | id_table2 | data_table3_1 | data_table3_2 |
---------------------------------------------------------
I'm selecting my table data from Table 3, and i join with Table 2. My question is, how can i get the data from Table 1 with other ways or join to get data_table1_1 without adding id_table1 in Table 3 ? Thankyou :)
hope this will work for you
$sql = "SELECT * from table 3
JOIN table 2 ON table 2.id = table 3.id
JOIN table 1 on table 1.id = table 2.id
";
$res = $this->db->query($sql);
Without data information gets difficult.
You can do it:
$this->db->select()
->from('table3')
->join('table2', 'table3.id_table2 = table2.id_table2', 'INNER')
->join('table1', 'table2.id_table1 = table1.id_table1', 'INNER')
->get()
->result_array();
Related
I am using CodeIgniter. I have an employee table and records are
id |firstname | lastname | mobileno | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2
Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | 2
But my expected output
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv
I have to display the name of created_by
I tried only this query.
$get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}
I have a hint (maybe this can give solution in your problem).
Try query like this :
SELECT
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'
With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0
Thanks
you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation
my data there will be a number key how to equalize with another table so it can be called names. i want to changes data (number key) to text from other table.
my table1 :
+-----------------------+
| ID | Name | Category |
-------------------------
| 1 | Home | 21 |
| 2 | Pro | 23 |
+-----------------------+
and table 2 :
+---------------------+
| ID | name_category |
-----------------------
| 21 | Sweet Home |
| 23 | your Home |
+---------------------+
how to get the same ID?. but the data will be shown in table 1
Try like this.Use join on table1.ID = table2.ID.displays all data of table1 with category names from table2 matching on table1 category ID.
$this->db->select(*)
->from('table1')
->join('table2', 'table1.ID = table2.ID');
$result = $this->db->get()->result_array();
print_r($result);//displays all data of table1 with category names from table2 matching on table1 category ID.
$this->db->query('select table1.* from table1 inner join table2 on table1.id = table2.id');
$this->db->result();
I have this schema
product_categories
id | product_category
---------------------
1 | ABC
2 | DBC
3 | EBA
store_product_categories
id | category_id | store_id
------------------------
1 | 2 | 11
2 | 1 | 11
3 | 3 | 11
I have created a query in mysql work bench
SELECT pc.* FROM product_categories pc LEFT JOIN store_product_categories spc ON pc.category = pc.id AND spc.store_id = 11 WHERE spc.category IS NULL;
This query actually gets all those categories from product_categories table which are not present in store_product_categories.
Now I am really really confused how to build this is Laravel Eloq..
I did try this.
$exclusive_categories = Product_category::join('store_product_categories','store_product_categories.category_id','=','product_categories.id')
->where('store_product_categories.store_id','=',session('store_id'))
->where('store_product_categories.category_id','=','NULL')->get();
But this doesn't give me result
Since you're joining on two different columns, you need to pass that through a function/closure:
$exclusive_categories = Product_category::leftJoin('store_product_categories', function($join) {
$join->on('store_product_categories.category_id','=','product_categories.id')
->on('store_product_categories.store_id','=',session('store_id'));
})
->where('store_product_categories.store_id','=','NULL')->get();
I'm not sure if this is quite what you want. If you're looking for where store_id is NULL OR store_id = the session id, you can pass that through another closure/function.
$exclusive_categories = Product_category::leftJoin('store_product_categories spc', function ($join) {
$join->on('spc.category_id', '=', 'product_categories.id');
$join->on('spc.store_id', '=', \DB::raw(session('store_id')));
})
->whereNull('spc.category_id')
->get(['product_categories.*']);
$exclusive_categories = Product_category::leftJoin('store_product_categories','store_product_categories.category_id','=','product_categories.id')
->where('store_product_categories.store_id','=',session('store_id'))
->whereNull('store_product_categories.store_id')->get();
https://laravel.com/docs/5.3/queries
User table:
userid | first_name | last_name |email |Password
1 | Tom | cruise |tom#gmail.com |d41d8cd98f00b204e9800998ecf8427e
2 | Tamera | Manzer |Tame#yahoo.com|d41d8cd98f00b204e9800998ecf8427e
3 | Vergie | Manzer |Vere#live.com |d41d8cd98f00b204e9800998ecf8427e
4 | Elmo | Milano |elmo#live.com |d41d8cd98f00b204e9800998ecf8427e
Connection Table
con_id | userid | connected_with |date
1 | 1 | 2 |2015-04-26
2 | 1 | 3 |2015-04-26
3 | 4 | 1 |2015-04-26
I want to make query to find connection of userid 1. In this 1 userid is connected with 2, 3, and also 4 so how can I find connection of userid 1
You can get your answer from here.
Read here
MySql Query.
SELECT Connection.connected_with, Connection.date
FROM Connection
JOIN User ON User.userid = Connection.userid
WHERE Connection.userid =1
Codeigniter Active Record
$this->db->select('connected_with', 'date');
$this->db->from('Connection');
$this->db->join('User', 'User.userid' = 'Connection.userid');
$this->db->where('userid', 1);
$this->db->get();
Like you said in comment, you have two foreign keys userid & connected_with, you can use union to combine both query result. First query you find the connection where Connection.userid=1. Second query you find the connection where Connection.connected_with=1. Then combine both result.
See the code below
SELECT Connection.userid AS 'Connection'
FROM Connection
JOIN User ON User.userid = Connection.connected_with
WHERE Connection.connected_with =1
UNION
SELECT Connection.connected_with
FROM Connection
JOIN User ON User.userid = Connection.userid
WHERE Connection.userid =1
Codeigniter Active Record
// First Query
$this->db->select('connected_with', 'date');
$this->db->from('Connection');
$this->db->join('User', 'User.userid' = 'Connection.userid');
$this->db->where('userid', 1);
$query = $this->db->get();
$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();
// Second Query
$this->db->select('userid', 'date');
$this->db->from('Connection');
$this->db->join('User', 'User.userid' = 'Connection.connected_with);
$this->db->where('connected_with', 1);
$query = $this->db->get();
$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();
// Union
$this->db->from("($subQuery1 UNION $subQuery2)");
$this->db->get();
Output
+--------------------------+
| Connection for User ID 1 |
+--------------------------+
| 4 |
| 2 |
| 3 |
+--------------------------+
I have four tables in MySQL.
+-----------------------------------------------+
Table 2 | Table 1 |
+============+=========+========+============+ +==========+=====================+
| ID | Name | Add | d_id | | ID | Details |
+============+=========+========+============+ +==========+=====================+
| 1 | ABC | City | 3 | | 1 | blah blah blah |
+------------+---------+--------+------------+ +----------+---------------------+
: : : : : : : :
|
+-------------------------+
|
Table 3 | Table 4
+============+==========+============+ +=========+======================+
| d_id | dis_Name | cat_id |----+ | cat_id | category_name |
+============+==========+============+ | +=========+======================+
| 1 | Myeloma | 5 | | : : :
+------------+----------+------------+ | +---------+----------------------+
: : : : +----| 5 | Cancer |
+---------+----------------------+
What I want to do is, I want to select all the rows in 'Table 1' those belongs to the 'category_name' in 'Table 4'.
Table 2 may contain multiple rows having same 'd_id'. Likewise, Table 3 may have multiple rows having same 'cat_id'.
If I select Category 'Cancer' then I would be getting multiple d_id's from table 3 in result. For each d_id, there will be multiple ID's in table 2. I want to fetch the details of these ID's (from Table 2) from Table 1. What would be the query statement???
Connectivity is shown in the table representaion.
you can achieve it doing this:
SELECT t1.*
FROM t4
INNER JOIN t3 ON (t4.cat_id = t3.cat_id)
INNER JOIN t2 ON (t3.d_id = t2.d_id)
INNER JOIN t1 ON (t2.id = t1.id)
WHERE t4.category_name = 'Cancer';
What you need is a join.
SELECT table.column_in_table
FROM table
INNER JOIN table_with_same_values
ON table.column_in_table = table_with_same_values.column_in_that_table;
I'm still not clear what you're trying to ask. Your question is a bit verbose. Some additional info here.
Hope this helps!