CodeIgniter how to sort joined record - php

I have following query in CodeIgniter:
$user = $this->db
->select('users.*, login_logs.ip_address, login_logs.date, login_logs.userid')
->from('db.users')
->join('db.login_logs', 'users.userid = login_logs.userid')
->group_by('users.userid')
->get()
->result_array();
It works very good but I have to select last record for each user( in login_logs table) but now I'm getting first record.
I have tried :
->order_by('login_logs.idlogin_logs', 'DESC')
but it didn't work.
I have no idea how to do it.
Is there any easy way to reach it?
#edit
I have triend ASC/DESC. No changes.
Present query:
SELECT `users`.*, `login_logs`.`ip_address`, `login_logs`.`date`, `login_logs`.`userid`
FROM `db`.`users`
JOIN `db`.`login_logs` ON `users`.`userid` = `login_logs`.`userid`
GROUP BY `users`.`userid`

Related

Laravel SQL missing data from Join table

I'm using the following code to create a query with a join.
$query = rs_vehicles::select(
'rs_vehicles.id',
'rs_vehicles.type',
'rs_vehicles.make',
'rs_vehicles.model',
'rs_vehicles.year',
'rs_vehicles.status',
'rs_vehicle_regs.registration'
);
$query ->leftJoin('rs_vehicle_regs', function($join){
$join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id')
->where('rs_vehicle_regs.registration_date', '=',
DB::raw("(select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)"));
});
$rsGrid = $query->get();
This produces the following sql statement:
select rs_vehicles.id, rs_vehicles.type, rs_vehicles.make,
s_vehicles.model, rs_vehicles.year, rs_vehicles.status,
s_vehicle_regs.registration from rs_vehicles
left join rs_vehicle_regs
on rs_vehicle_regs.rs_vehicles_id = rs_vehicles.id
and rs_vehicle_regs.registration_date = (select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)
The SQL statement generated by Laravel runs perfect when I execute it in MySQL Workbench and it brings back the expected values for ALL fields. However when I execute the code in Laravel although the script runs perfectly with no errors and brings back almost all the required data the 'rs_vehicle_regs.registration' field which is in the joined table comes back as null. I've spent ages trying to figure this out and I am getting nowhere. Anybody any idea why this wont work in Laravel? I'm at my wits end.
I have tried changing get() to toSql() and I get the following
select `rs_vehicles`.`id`, `rs_vehicles`.`type`, `rs_vehicles`.`make`, `rs_vehicles`.`model`, `rs_vehicles`.`year`, `rs_vehicles`.`vehicle_status`, `vh_type`.`display_text` as `type_text`, `vh_status`.`display_text` as `vehicle_status_text`, `rs_vehicle_regs`.`registration` as `registration` from `rs_vehicles` left join `app_params` as `vh_type` on `rs_vehicles`.`type` = `vh_type`.`list_value` and `vh_type`.`param_type` = ? left join `app_params` as `vh_status` on `rs_vehicles`.`vehicle_status` = `vh_status`.`list_value` and `vh_status`.`param_type` = ? left join `rs_vehicle_regs` on `rs_vehicle_regs`.`rs_vehicles_id` = `rs_vehicles`.`id` where `rs_vehicles`.`status` = ?
I have also tried the following:
$query ->leftJoin('rs_vehicle_regs', function($join){
$join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_employees.rs_vehicles_id')
->where('rs_vehicle_regs.registration_date', '=', 'max(registration_date)');
});
This produces the exact same result as the original query where the object as the field but the values are null.
Then I tried this:
$query ->leftJoin('rs_vehicle_regs', 'rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id');
This join works and brings back the data I am looking for however if the join table has more than one entry related to the parent table as can happen I get all records returned and I only want one record were max(registration_date). This has me totally stumped.

Codeigniter SQL query no records in other table

I would like to join 2 tables but there is no record in the other table.
For example
Table name: shop_items
shop_item_id
shop_item_sk
shop_item_name
Table name: item_reviews
item_review_id
item_review_sk
item_review_desc
item_review_rate
shop_item_sk
item_review_by
The problem is that it the item details doesn't appear when it don't have any record in the item_reviews table
this is my SQL statement
$query = $this->db->select('*')
->from('shop_items')
->join('item_reviews', 'shop_items.shop_item_sk=item_reviews.shop_item_sk','inner')
->where('shop_items.shop_item_sk',$id)
->limit(1)
->get();
return $query->result();
I researched about UNION but I don't know how to use it in Codeigniter.
Advance thank you for those who can help me.
Use left outer join. Here shop_items table is the left table join item_reviews with it.
Maybe with a left join ?
$query = $this->db->select('*')
->from('shop_items')
->join('item_reviews', 'shop_items.shop_item_sk=item_reviews.shop_item_sk','left')
->where('shop_items.shop_item_sk',$id)
->limit(1)
->get();
return $query->result();

get user with most articles and its amount with active record or query

I want to get the user that wrote the most articles. I do so fine in two ways with ActiveRecord like the following:
$table = Articles::find()
->select('articles.*, COUNT(*) AS cnt')
->with('user','userDetails')
->groupBy('articles.user_id')
->orderBy(('cnt DESC'))
->limit(10)
->offset($offset)
->all();
and with a query like the following:
$query = (new Query())
->select('articles.user_id, COUNT(*) AS num_articles')
->from('articles')
->join('LEFT JOIN', 'user_details', 'user_details.user_id = articles.user_id')
->groupBy('articles.user_id')
->orderBy('num_articles DESC')
->limit(10)
->offset($offset)
->all();
The problem is that the ActiveRecord gives me further needed informations userDetails that I need. But I do not get the amount of articles of user that should be on cnt
With the Query I get the user_id and the amount of articles. But I do not get it working by joining with userDetails. All of these does not work: LEFT JOIN, RIGHT JOIN, INNER JOIN.
I am interested in resolving both for learning, but for concrete I need help with the ActiveRecord problem.
Okay well I solved it for the ActiveRecord. The ActiveRecords needs a public $var; in the Model. So to get the amount you have to add the mentioned public... to your Model so that in my case:
public $cnt; extends the ActiveRecord of Articles
now I can access it with the given Request in my Question. But this just solves the first point. I am still interested in the second way for more complex Queries.
I dont have much idea about active record but I think the below is something what you are looking for
select * from user_details where user_id in
(select A.user from
(select user_id as user, COUNT(*) AS num_articles
from articles group by user_id order by num_articles desc LIMIT 10)A
);
for second point you should include required column from joined table to select statement:
$query = (new Query())
->select('articles.user_id, COUNT(*) AS num_articles, user_details.username as username')
->from('articles')
->join('LEFT JOIN', 'user_details', 'user_details.user_id = articles.user_id')
->groupBy('articles.user_id')
->orderBy('num_articles DESC')
->limit(10)
->offset($offset)
->all();

Query for getting values from multiple tables

I have a table video with fields videoid, genre(int-foreign key), language(int-foreign key) etc. I want to get the value of genre from genre table and language from movie_languages table.
The structure of tables are given below:
video
genre
movie_languages
How can I join these 3 tables to get the language and genre related to each videos in video table. Also when user didn't select genre/language in the form, value 0 will be inserted to the table. Will this affect the query. I am using codeingiter and I tried with the following query and is not working.
$this->db->select('video.*,movie_languages.language,genre.genre');
$this->db->join('genre', 'video.genre = genre.id');
$this->db->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();
Please help me.
Thanks in advance.
It will be, you need a left join in this case
Try this
$query = $this->db
->select('v.*,ml.language,g.genre')
->from('video as v')
->join('movie_languages AS ml', 'v.language = ml.id', 'left outer')
->join('genre AS g', 'v.genre = g.id', 'left outer')
->get();
try this
$this->db->select('video.*,movie_languages.language,genre.genre')
->from('video')
->join('genre', 'video.genre = genre.id')
->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

Codeigniter Active Record / MySQL Join Query - How to Return Results if one of the Table Rows is Not Present

I have the following query:
$this->db
->select('SQL_CALC_FOUND_ROWS null as rows
,table1.*
,table2.*
,table3.*', FALSE)
->from('table1')
->where('table1.column1', $user_id)
->join('table2', 'table2.column2 = table1.column2')
->join('table3', 'table3.column2 = table1.column2')
->group_by('table1.column2')
->order_by('table1.column2', 'DESC');
$query = $this->db->get();
The problem is, there may not be a row in table 3 and if there is not, I would still like to return a result with the remainder of the query data. Please could someone advise how to achieve this?
you should do a left join on table3
Use left join and also use group_by to get exact records:
$this->db->join('login_ranknames AS t4', 't4.id = t1.rank', 'left');
$this->db->group_by('t4.id');

Categories