I'm working on Codeigniter project and came across a problem. I would like that my function on my model to bring me the data from the POST table, where POST_ID in CATEGORY Table is not null, without joining the table, so (do not bring back the data whose POST_ID in CATEGORY table not exists).
so i tried this method how can i get this result?
code: // MyModel
function get_data() {
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
if ($id != null) {
$this->db->where('ID',$id);
$result = $this->db->select('POST.ID,POST.DATE,'
. ' TYPE.Post_Type P_TYPE,)
->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left')
->order_by('POST.ID', 'DESC')
->where_in('POST.Post_Type = type_1, type_2')
->where("NOT EXISTS(SELECT POST_ID FROM CATEGORY WHERE POST.ID=CATEGORY.POST_ID )", FALSE)
->get('POST');
}
return $result;
}
Thank you for help
It would be better to use IN if you are trying to have a data where is not null ( meaning it exists ) or you can also change it to NOT IN if the answer you are looking for is the other way around. anyway, i did not change anything in your query, i just put the POST_ID and IN in the where statement
function get_data() {
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
if ($id != null) {
$in = array('type_1', 'type_2');
$this->db->where('ID',$id);
$this->db->select('POST.ID,POST.DATE,TYPE.Post_Type as P_TYPE', FALSE);
$this->db->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left');
$this->db->order_by('POST.ID', 'DESC');
$this->db->where_in('POST.Post_Type', $in);
$this->db->where("POST.POST_ID IN(SELECT C.POST_ID FROM CATEGORY C )");
$this->db->from('POST');
$result = $this->db->get();
}
return $result;
}
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();
}
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;
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.