Here is my codes:
public function getallcontractfiles()
{
$this->db->select('cd.*, GROUP_CONCAT(cf.Contract_File_Name) AS fileslink');
$this->db->from('contract_details as cd');
$this->db->join('contract_files as cf', 'cd.Contract_Id = cf.Contract_Id','LEFT');
if($this->session->userdata['user_type'] == 'ADMIN' && $this->session->userdata['user_group'] == '' ){
$this->db->where('cd.Company_id',$this->session->userdata['company_id']);
}
if($this->session->userdata['user_type'] == 'USER'){
$this->db->where('cd.users_id',$this->session->userdata['logged_user']);
}
$this->db->group_by('cd.Contract_Id ');
$this->db->order_by('cd.Contract_Id', 'desc');
$query = $this->db->get();
// print_r($this->db->last_query()); die;
if ( $query->num_rows() > 0 )
{
foreach($query->result() as $row){
$rows[] = $row;
}
return $rows;
}
}
My question is: I have also a table called company and my App is all about to print out all contracts files with their companies. I have defined Company_id as a foreign key in Contract_files.
Please help me to join company table to this function.
Thank you.
public function getallcontractfiles(){
$this->db->select('cd.*, GROUP_CONCAT(cf.Contract_File_Name) AS fileslink');
$this->db->from('contract_details as cd');
$this->db->join('contract_files as cf', 'cd.Contract_Id = cf.Contract_Id');
if($this->session->userdata('user_type') == 'ADMIN' && $this->session->userdata('user_group') == '' ){
$this->db->where('cd.Company_id',$this->session->userdata('company_id'));
}
if($this->session->userdata('user_type') == 'USER'){
$this->db->where('cd.users_id',$this->session->userdata('logged_user'));
}
$this->db->group_by('cd.Contract_Id ');
$this->db->order_by('cd.Contract_Id', 'desc');
$query = $this->db->get();
// print_r($this->db->last_query()); die;
if ( $query->num_rows() > 0 )
{
foreach($query->result() as $row){
$rows[] = $row;
}
return $rows;
}
}
Try This Code.
I'm not familiar with your ORM but this should make sense:
$this->db->join('company', 'cf.Company_id = company.Company_Id','INNER');
Right after your join with contract_files table, Also specify fields you want to retrieve in the from company table as well.
UPDATE
Since your query is bringing up multiple contract files then you will have to do the same with company table as every contract file could have many companies, If they all have the same company, Then the foreign key Company_id should be in contract_details table.
IMPORTANT
GROUP_CONCAT() has a limitation of 1024 by default, So it's not the best idea to put files names in it.
LEFT JOIN table2 ON table1.column_name = table2.column_name;
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
INNER JOIN table2 ON table1.column_name = table2.column_name;
Related
so i kinda new with codeigniter and im trying to join 3 table from my database
database 1 : dkm (id, tgl, ref, etc)
database 2 : order_product (kode_barang, packing, nama_barang, etc)
database 3 : product (kodeprod, tglpakai, etc)
im already trying what other ppl do to join more than 2 table in codeigniter but i got this error :
Error Number: 1066
Not unique table/alias: 'order_product'
SELECT *
FROM `order_product`
JOIN `order_product` ON `order_product`.`kode_barang` = `dkm`.`id`
JOIN `order_product` ON `order_product`.`kode_barang` = `produksi`.`kodeprod`
This is my code :
Bukaka_model.php
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('order_product','order_product.kode_barang = dkm.id');
$this->db->join('order_product','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
You're trying to join to the same table multiple times, instead you need to join to the other tables once each.
You just need to change the names of the table you're joining to:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
Try this,
Here, you have a mistake in joining tables, in CI join() in the first parameter you need to pass/write table name with you want to join
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}else{
return array();
}
}
Try this:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','dkm.id= order_product.kode_barang');
$this->db->join('produksi',' produksi.kodeprod = order_product.kode_barang');
$query = $this->db->get();
return $query->result();
}
I have 3 tables in database:
teams
id
name
matches
id (int)
team_home_id
team_away_id
goals
id
match_id
team_id
time
I need display names of teams in view where I get goals in controller.
I know that I should do join tables.
I have this code:
public function get_goals() {
$this->db->select('goals.*');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$q = $this->db->get();
return $q->result();
}
and I don't know what next.
I need in view display names by:
$goals->team_home_name and $goals->team_away_name
To get $goals->team_home_name and $goals->team_away_name result, use aliases like this :
public function get_goals() {
$this->db->select('goals.*, home_team.name team_home_name, away_team.name team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams home_team', 'home_team.id = matches.team_home_id');
$this->db->join('teams away_team', 'away_team.id = matches.team_away_id');
$q = $this->db->get();
return $q->result();
}
You can try with this:-
public function get_goals() {
$this->db->select('g.* , t.name as team_home_name , t.name as team_away_name);
$this->db->from('goals as g');
$this->db->join('matches as m', 'm.id = g.match_id');
$this->db->join('teams as t', 't.id = g.team_id');
$q = $this->db->get();
return $q->result();
}
U can use an alias by
guss the team_away_name and team_home_name from teams table
public function get_goals() {
$this->db->select('goals.*, team.team_home_name, team.team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams as team', 'team.id = goals.team_id');
$q = $this->db->get();
return $q->result();
}
I have this method in my Model
function get_users_details(){
$this->db->select("a.*,sum('b.downloads') as downloads,COUNT('b.user_email') as uploads");
$this->db->from('user a');
$this->db->join('files b', 'a.email = b.user_email','inner');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row){
$data[] = $row;
}
$query->free_result();
return $data;
}
else{
return false;
}
}
It only returns values from a single row while actually it supposed to return values from multiple rows.
sum() and count() are aggregate functions and will only return 1 row unless you combine it with a group_by statement.
SELECT count(*) FROM table_a
will return the total number of rows in table_a.
SELECT table_a.email, count(*) FROM table_a GROUP BY table_a.email
will return the total number of rows PER email address.
In codeigniter 3, we use
$this->db->group_by("table_a.email");
First I'm really new to fuelphp, you can down vote the question if needed.
My problem is that i made a facebook similar wall, and i don't really understand the comments logic.
So i tried to join my tables this way
static function get_stream()
{
$query = DB::select()->from('stream_post');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->join('stream_comment');
$query->on('stream_post.stream_id', '=', 'stream_comment.stream_id');
$query->order_by('stream_post.stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
}
the problem with this is that, this only shows the stream posts what have comments, and doesn't show the others.
So can please someone give me a logic how to join the tables to show those post to what doesn't have a comment?
Try that:
static function get_stream()
{
$query = DB::select()->from('stream_post');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->join('stream_comment', 'RIGHT'); // The RIGHT JOIN keyword returns all rows from the right table, even if there are no matches in the left table.
$query->on('stream_post.user_id', '=', 'stream_comment.user_id');
$query->order_by('stream_post.stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
}
Edit:
That query should work (when every user_id from stream_post has the same user_id in users_metadata. Just transfer this query to fuelphp (I didn't use it before).
SELECT *
FROM stream_post
RIGHT JOIN stream_comment
ON stream_post.stream_id = stream_comment.stream_id
JOIN users_metadata
ON stream_post.user_id = users_metadata.user_id
ORDER BY stream_post.stream_id DESC
I have 4 tables on my DB;
merchants, pay_method, spendings ans transaction_type.
They all have an id and a title, except spend (spend have the Foreign keys of the others and other field like user_id, comment, ... ).
I try to have all the informations of all the tables for an unique user_id!
for perform it, I tried a little test that doesn't run :
public function getSpendingsForUser( $userid )
{
$mercTable = Engine_Api::_()->getDbTable('merchants', 'spendings');
$mercName = $mercTable->info('name');
$table = Engine_Api::_()->getDbTable('spendings', 'spendings');
$spendName = $table->info('name');
$select = $table->fetchAll(
$table->select()
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
return $select;
}
An in the view file :
foreach($this->spendings as $s) {
var_dump($s->comment);
}
How can I make the join correctly to have all the info from the user_id ???
Thanks a lot !!!
You should define doesn't run to help us help you. But I can tell you you probably need to ->setIntegrityCheck(false);
$select = $table->fetchAll(
$table->select()
->setIntegrityCheck(false);
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
Before the join.