can you help I have two tables images and note. I would like to get all items from these tables and order by date. It is possible I am using active record in codeigniter. Tables are independent.
Thank you for replies.
this should work:
$this->db->orderby("date", "ASC"); //or desc
$query = $this->db->get('images'); //or $query = $this->db->get('note');
$result=$query->result_array();
print_r($result);
or if you want use union all
$this->db->query('SELECT * FROM (SELECT id, date FROM images UNION ALL SELECT id, date FROM note) result ORDER BY result.date');
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order from http://www.w3schools.com/sql/sql_union.asp.
I think you want a Cross Join.
Here is your codeigniter code
$this->db->from("images ,note");
$this->db->select("images.*,note.*);
//make sure both dont have same column name other wise use this
//$this->db->select("images.column1,images.column2,note.column1);
$this->db->orderby("images.date", "DESC");
//you can add more orderby
//you can add where condition too
$result=$this->db->get()->result_array();
Now you will get the cross product.
Hope it will help you.
In which table do you have date column? I assume you have it in images table.
$this->db->select('images.coulmn1, images.culmn2, images.date_col, note.col1, note.col2');
$this->db->from('images');
$this->db->join('note', 'note.key1 = images.key1); //key1 is key field to relate both table.
$this->db->order_by("images.date_col", "desc");
$result = $this->db->get()->result_array();
Hope it will help.
Try this:
$this->db->select('*');
$this->db->from('images');
$this->db->join('note');
$this->db->orderby("date column name", "ASC");
$query = $this->db->get();
$result=$query->result_array();
print_r($result);
Related
i am joining three tables and applying group by to pick a single distinct row of each type. when i join with table marketing_follow i have multiple entries and to pick one i am using group by . now i want to pick the entry with most recent date in follow up table . i've been tryn to get this done for quite some time now and really need some help
public function getRegionWiseUserDetails($array){
$this->db->select('mg.region_name, mu.contact_person, mu.contact_number, mu.status, mu.company_name, mu.user_type, mf.follow_up_date, mf.date, mu.user_id');
$this->db->from('marketing_users as mu');
$this->db->join('marketing_group as mg','mg.id = mu.region_id','left');
$this->db->join('marketing_follow_up as mf','mf.user_id_fk =
mu.user_id','left');
foreach($array as $condition){
$this->db->or_where('mg.id',$condition);
}
$this->db->order_by('mf.follow_up_date','asc');
$this->db->group_by('mf.user_id_fk');
$query = $this->db->get();
return $query->result_array();
}
You need to sort the date field in desc order. Also for asc we dont have to pass it as second paramter it defaults accept asc
$this->db->order_by('mf.follow_up_date','desc');
is it possible to pass the variable value in mysql query in codeigniter ?
I want to select records from database that have id same as my session id.
my code is :
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('products');
$this->db->where('id_admin = (SELECT id_admin FROM users WHERE id='.$this->session->userdata('user_id').')', NULL, TRUE);
can we concatenate variable values in database queries?
This code not given any error, but not give any result also.
Use join in active records
$this->db->select('p.*');
$this->db->from('products as p');
$this->db->join('users','p.id_admin=users.id');
$this->db->where('users.id',$this->session->userdata('user_id'),false);
$query = $this->db->get();
An Example Reference of how to write the JOIN Query in CI as per the Documentation of the Active Records.
$article_id = $this->input->post('article_id');
$this->db->select('*');
$this->db->from('articles');
$this->db->join('news', 'news.id = articles.id');
$this->db->where('articles.id',$article_id);
$query = $this->db->get();
The above code will produce the query as follows for execution.
SELECT * FROM articles JOIN news ON news.id = articles.id WHERE articles.id='10'
Provided the passed id is 10
In order to view the Result of the executed query you must perform the below code.
print_r($query->result());
Try like this..use joining of two tables.
<?php
$this->db->select('products.*');
$this->db->from('products');
$this->db->join('users','products.id_admin=users.id_admin');
$this->db->where('users.id',$this->session->userdata('user_id'));
$query = $this->db->get();
print_r($query->result_array());//your array of records
have a table with a "tag" column - I want to select 10 random records from my table where none of the ten records share the same tag (each record has a unique tag). How would I do this? This is my current query:
$this->db->select('extra_imagery');
$this->db->where('cat', '4');
//need something like: $this->db->where('tag',IS UNIQUE);
$this->db->limit($this->config->item('imagery-limit'));
$this->db->order_by('extra_imagery.id','RANDOM');
Try this
$query = $this->db->query("SELECT DISTINCT extra_imagery FROM table_name WHERE cat = 4 GROUP BY RAND() id");
$result = $query->result_array();
return $result;
In order to avoid repeats, you can use DISTINCT and/or a GROUP BY to your query.
As per what the manual states:
https://ellislab.com/codeigniter/user-guide/database/active_record.html
For example:
$this->db->distinct();
and/or
$this->db->distinct();
Other references (MySQL):
http://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
http://dev.mysql.com/doc/refman/5.7/en/select.html
I have two tables 'accounts_transactions' and 'accounts_bills_transactions'.
I have to left join these two using active record of codeigniter.But the names of key columns used to join are different.So I am not getting the key column from the left table in the output .What query should I write to get the key column from the left table included in the result.
My code is
$this->db->select('*');
$this->db->from('accounts_transactions');
$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_bills_transactions.transaction_id','left');
$query = $this->db->get();
So, as you see the key columns used to join here are , id from left table and transaction_id from second table.The problem is that I am not getting the id from left table in the result.But I am getting all other columns.I assume the problem is because of difference in column names used to join.ie both the column names are not named 'id' .So how can I get the id from left table included in the result.
You could alias them:
$this->db->select('accounts_transatctions.*, account_transactions.id AS a_id,
accounts_bills_transactions.*,
account_bills_transactions.id AS ab_id');
$this->db->from('accounts_transactions');
$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_transactions.transaction_id','left');
$query = $this->db->get();
The two IDs will now be available as a_id and ab_id (or whatever alias you choose)
Note: I'm not sure if you can alias in AR without avoiding escaping (haven't been using CI for a while). Should you get any error for that reason, just pass false as second parameter of $this->db->select():
$this->db->select('...', false);
you can try this if you confuse of using $this->where or $this->join
$query = $this->db->query("select ......");
return $query;
You problem is so simple. You can use this query
$query = $this->db
->select('at.*')
->select('abt.id as abt_id');
->from('accounts_transactions at');
->join('accounts_bills_transactions abt', 'at.id = abt.transaction_id','left');
->get()
->result();
When same column are used in join it selects only one. You need to give alise to the other column in second table. The best practice is to use a structure like this
accounts_transatctions
--------------------------
accounts_transatctions_id
other_columns
accounts_bills_transactions
---------------------------
accounts_bills_transactions_id
accounts_transatctions_id
other_columns
I have a table with this structure:
opinion_id, author_id, title, content
I would like to get all the latest records from the table, one record for author, that means the latest record for every author...
My distinct function does not seem to be working...
function getOpinions() {
$data = array();
$this->db->select('*');
$this->db->from('opinions');
$this->db->join('authors', 'opinions.author_id = authors.author_id');
$this->db->order_by('date', 'desc');
$this->db->distinct('author_id');
$Q = $this->db->get();
if ($Q->num_rows() > 0) {
foreach($Q->result_array() as $row) {
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
In Codeigniter, distinct does not work the way you expect it by field name. If you look at the manual - there is no argument for distinct. If you look at the code, it only takes a boolean, which defaults to true. It just adds the DISTINCT keyword to the query after the SELECT keyword. That's it.
In your case, I think it would be better to use a GROUP BY as in
$this->db->group_by('opinions.author_id');
Hopefully the order by would work as per your need in this instance by ordering before the grouping.
Cheers!
EDIT - update after OP comments
I know the ordering can be messed up - I sort of mentioned it :)
Anyway, I might be assuming some of your table structure here, but this would force the GROUP BY to pick the rows on the top. I assume that the date is on the opinions table and you only want the latest row from that with author details.
SELECT * FROM `authors`
JOIN (
SELECT * FROM opinions
ORDER BY `date` DESC
) AS ops ON ops.author_id = authors.author_id
GROUP BY ops.author_id
You will not be able to construct this query on active record though. Hope this helps.