How to get Last Message from chat conversation? - php

Hi i have a table name messages i want to get last message from chat conversation between two users.
Table -
id from_id to_id text created
========================================
1 1 2 hi 2014-12-01 10:30:10
2 2 1 Hello 2014-12-01 10:30:20
3 3 1 Hi chinu 2014-12-01 10:32:02
4 3 1 hw r u? 2014-12-01 10:32:22
5 2 1 h r u? 2014-12-01 10:33:01
Please check my query =
//$user_id=$this->Session->read('Auth.User.id');
$user_id=1;
$fields=array('DISTINCT(Message.from_id), Message.id, Message.text, Message.created');
$this->Paginator->settings = array(
'conditions'=>array('Message.to_id'=>$user_id),
'limit' => 30,
'order' => array(
'Message.created' => 'DESC',
'Message.id' => 'DESC'
),
'fields'=>$fields,
'group'=>array('Message.from_id')
);
$inbox = $this->Paginator->paginate('Message');
pr($inbox);
$this->set('inbox', $inbox);
I want to get last message conversation between 2 and 3 id. The output looks like -
id from_id to_id text created
========================================
5 2 1 h r u? 2014-12-01 10:33:01
4 3 1 hw r u? 2014-12-01 10:32:22
Please help me. If you don't know the cakephp please post core php MySQL query here.
Thanks
chatfun

Please check this query -
SELECT * FROM (SELECT DISTINCT * FROM messages WHERE (from_id=1 OR to_id=1) ORDER BY created DESC) as m GROUP BY `from_id`

I suppose this query would work (although I don't understand why 'to_id' = 1 in your expected output, maybe I didn't get your purpose right)
SELECT 'id', 'from_id', 'to_id', 'text', 'created'
FROM Message
WHERE ('from_id' = 2 AND 'to_id' = 3) OR ('from_id' = 3 AND 'to_id' = 2)
ORDER BY created DESC
LIMIT 1

This how the query looks like to achieve what you are looking at , i.e. getting the last conversation from each from_id
select t1.* from Message
t1 left join Message t2
on t1.from_id = t2.from_id
and t1.id<t2.id where t2.id is null;
You have a primary key id auto_incremented so it could be used in the comparison instead of date.
Or
select t1.* from Message t1
where not exists (
select 1 from Message t2 where t1.from_id = t2.from_id and t1.id < t2.id
);

Related

nested where condition in codeigniter

from this data how can i fetch all users where user_id in tbl_attendance not having current date with application_status 3 and 4
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
my code is like this
$date=date('Y-m-d');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" )');
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
want to combine those two lines and produce the output like if current date is present in 3 or 4 please help me
You can try like this.To combine two conditions
$this->db->where("tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='$date' AND tbl_attendance.attendance_status IN(3,4))");
seeing you're using query builder...
$this->db->group_start()
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
$this->db->where_not_in('tbl_users.user_id', 'SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in = '. $date, false)
$this->db->group_end()
should work

MySql - count from beginning until each day

Let's say I have the following table (keep in mind that this table will have 10000+ rows):
id total date
1 5 2015-05-16
2 8 2015-05-17
3 4 2015-05-18
4 9 2015-05-19
5 3 2015-05-20
I want the query to give the following result:
1
date => 2015-05-16
total => 5
2
date => 2015-05-17
total => 13
3
date => 2015-05-18
total => 17
4
date => 2015-05-19
total => 26
5
date => 2015-05-20
total -> 29
I can't think of any query that would do this right now, that's why I am not providing any code that I have tried.
Any thoughts? I am not sure if this is possible only with mysql, maybe I have to use and php.
This could be done using user defined variable in mysql and then get the running total as
select
id,
total,
date
date from
(
select
id,
#tot:= #tot+total as total,
date from my_table,(select #tot:=0)x
order by date
)x
You can do this -
SELECT
a.id,
a.date,
(SELECT SUM(b.total) FROM your_table WHERE b.date <= a.date) as new_total
FROM your_table a, your_table b
ORDER BY a.date ASC
This should do it:
select id, (select sum(total) from table a where a.date <= b.date) from table b

Trying to group up results of a SQL multi table query

alright best way for me to explain is by giving an example
Table 1
ID | name | class
-------------------------
1 | first name | a
2 | second name | b
Table 2
ID | name_id | meta_key | meta_value |
-------------------------------------------------------
1 | 1 | image | someImage.jpg |
2 | 1 | description | very long description |
3 | 2 | image | someImage_2.jpg |
4 | 2 | description | very long description 2 |
I am trying to select name and class from Table 1, and also all the records from Table 2 that has the same name_id as ID (in Table 1).
Here is the query i have now:
SELECT
table1.name,
table1.class,
table2.name_id,
table2.meta_key,
table2.meta_value
FROM
table1
LEFT JOIN
table2 ON ( table1.ID = table2.name_id )
WHERE
table1.ID = '2'
This works and return an array with as many elements as the Table 2 entries that has name_id = 2
Is there a way to return an array with 1 the result from first table, and another array with results from the second table ?
Update:
The ideal results will be:
$result['table1'][0] = array('name' => 'second name',
'class' => 'b')
$result['table2'][0] = array('name_id' => 2,
'meta_key' => 'image',
'meta_value' => 'someImage_2.jpg')
$result['table2'][1] = array('name_id' => 2,
'meta_key' => 'description',
'meta_value' => 'very long description 2')
hopes that makes sense.
I'd split the result array AFTER fetching it from the database...
//Assuming your result-set is assigned to $result:
$table1_keys = array('id', 'name', 'class');
$table2_keys = array('id', 'name_id', 'meta_key', 'meta_value');
$table1_results = array();
$table2_results = array();
foreach($result as $key => $val) {
if(in_array($key, $table1_keys)) {
$table1_results[] = $val;
}
if(in_array($key, $table2_keys)) {
$table2_results[] = $val;
}
}
After this, your result-set should be split into two arrays: $table1_results and $table2_results.
Hope this helps you.
I don't believe there's a way to do it in SQL but I'd label each table so it can be done generically in PHP.
SELECT
':table1',
table1.name,
table1.class,
':table2',
table2.name_id,
table2.meta_key,
table2.meta_value
FROM ...
Although personally I'd just do this.
SELECT 'table1', table1.*, ':table2', table2.*
FROM ...
That makes it easier to duplicate entire rows between client and server and reduces maintenance when you decide to add a new field.
However in your query you are duplicating table1 for each row returned so perhaps this would be better.
SELECT
table1.*,
table2a.meta_value AS image,
table2b.meta_value AS description
FROM
table1
LEFT JOIN
table2 AS table2a ON ( table1.ID = table2a.name_id AND table2a.meta_key = 'image' )
LEFT JOIN
table2 AS table2b ON ( table1.ID = table2b.name_id AND table2b.meta_key = 'description' )
WHERE
table1.ID = '2'

How can you count matches from a join action in CakePHP?

I want to count to an extra field created, the matches from a join. Is this possible in CakePHP?
I have an example of my data that I curently have.
And how would a query look in mySQL for this type of result?
Table:goal
id | name
-----------
1 Goal X
2 Goal Y
Table: tasks
id | name | goal_id
-------------------
1 task1 1
2 task2 1
3 task3 2
4 task4 2
5 task5 2
Result
id | name | matches
-------------------
1 goal1 2
2 goal2 3
MySQL query :
SELECT goal.id, goal.name, Count( * ) AS matches
FROM goal
RIGHT JOIN task ON goal.id = task.goal_id
GROUP BY goal.id
CakePHP : [if you have model with name Goal and Task]
$options['fields'] = array(
'Goal.id',
'Goal.name',
'count(*) AS matches'
);
$options['joins'] = array(
array(
'table' => 'tasks',
'alias' => 'Task',
'type' => 'Right',
'conditions' => array(
'Goal.id = Task.goal_id'
)
)
);
$options['group'] = array('Goal.id');
$result = $this->Goal->find('all', $options);
mysql should be that:
Select goal.id,goal.name,Count(*) From goal RIGHT JOIN tasks on goal.id=tasks.goal_id Group by goal.id
CakePHP, can't tell you without testing...

CodeIgniter SQL query - how to sum values for each month?

I have the following table:
//table_1
record_id user_id plant_id date cost
1 1 1 2011-03-01 10
2 1 1 2011-03-02 10
3 1 1 2011-04-10 5
4 1 2 2011-04-15 5
I would like to build a query (if possible using CI Active Records, but MySQL is fine) in which I generate the following result:
[1] => [1] => [March 2011] [20]
=> [April 2011] [5]
[2] => [March 2011] [0]
=> [April 2011] [5]
I have tried using $this->db->group_by but I think I'm not using it correctly.
If anyone could give me a pointer or roadmap to get this done it would be much appreciated -- thanks!
Sample table
drop table if exists t;
create table t( record_id int, user_id int, plant_id int, date datetime, cost float);
insert t select
1 ,1, 1 ,'2011-03-01', 10 union all select
2 ,1, 1 ,'2011-03-02', 10 union all select
3 ,1, 1 ,'2011-04-10', 5 union all select
4 ,1, 2 ,'2011-04-15', 5;
Because you want to see the row with 0, you need to do a cross join between the year-month and all user-plants.
select up.user_id, up.plant_id, ym2, ifnull(sum(t.cost),0) totalcost
from (select distinct date_format(date, '%Y-%m') ym, date_format(date, '%M %Y') ym2 from t) dates
cross join (select distinct t.user_id, t.plant_id from t) up
left join t on date_format(t.date, '%Y-%m') = dates.ym
and up.user_id=t.user_id
and up.plant_id=t.plant_id
group by up.user_id, up.plant_id, ym2, ym
order by up.user_id, up.plant_id, date(concat(ym,'-1'));
The fact that Month Year does not sort correctly also requires the complex treatment of the dates for ordering purposes at the end.
This is a pretty intense way to do it and it would be far preferable to store the month-year as a column itself if you need to make these queries frequently, but this is basically how it works:
SELECT CONCAT(MONTHNAME(date), ' ', YEAR(date)) AS monthyear, COUNT(*) AS count GROUP BY YEAR(date), MONTH(date), plant_id;
That should get you the resultset you're looking for.

Categories