I have user_messages table with columns id, sender_id, receiver_id, message, deleted
I have to retrieve all messages like this
SELECT *
FROM
user_messages UserMessages
WHERE (
UserMessages.deleted = false
AND (
(sender_id = $loggedin_user_id AND receiver_id = $user_id)
OR
(sender_id = $user_id AND receiver_id = $loggedin_user_id)
)
ORDER BY
created DESC
Currently, I'm using this query builder
$message_by_list = $this->UserMessages->find()
->where(['UserMessages.deleted' => false])
->andWhere(function ($exp) {
return $exp->or_([
'sender_id' => $this->Auth->user('id'),
'receiver_id' => $this->Auth->user('id')
]);
})
which is generating sql query as
FROM
user_messages UserMessages
WHERE
(UserMessages.deleted = false AND (sender_id = $loggedin_user_id OR receiver_id = $loggedin_user_id))
ORDER BY
created DESC
How to write optimized ORM Query to retrieve data as above?
Edit 2 : Updated query for arilia's answer
WHERE (
(
UserMessages.deleted = false
)
AND
(
(
UserMessages.sender_id = $loggedin_user_id
AND
UserMessages.receiver_id = $user_id
)
OR
UserMessages.sender_id = $user_id
OR /// doubt here
UserMessages.receiver_id = $loggedin_user_id
)
)
try this
$message_by_list = $this->UserMessages->find()
->where(['sender_id' => $user_id, 'receiver_id' => $logged_user_id])
->orWhere(['sender_id' => $logged_user_id, 'receiver_id' => $user_id])
->andWhere(['UserMessages.deleted' => false]);
Related
Here i have two table,i have to join these two table and i have to get the plan details, i tried but is not happening, here is my code
user_info
id fullName
1 Arun
2 Sarvan
user_active_plan
id userId planName
1 1 Free Plan
2 1 Cool Plan
3 2 Free Plan
contact_property
id userId contactProperty
1 1 A
2 1 B
3 2 C
Here user_info(tablename) id (column name) i am using foreign key of user_active_plan(table name) userId(column name)
I want get the latest plan based on userId,So i am using desc order , but it is not coming expected results:
$sql = "SELECT a.fullName,b.*FROM user_info a LEFT JOIN user_active_plan b ON a.id = b.userId GROUP BY b.userId ORDER BY id DESC";
$result = $this->GetJoinRecord($sql);
print_r($result);
I am getting the following incorrect results:
Array
(
[0] => Array
(
[fullName] => Sarvan
[id] => 3
[userId] => 2
[planName] => Free Plan
)
[1] => Array
(
[fullName] => Arun
[id] => 1
[userId] => 1
[planName] => Free Plan
)
)
)
I was expecting the following:
Array
(
[0] => Array
(
[fullName] => Sarvan
[id] => 3
[userId] => 2
[planName] => Free Plan
)
[1] => Array
(
[fullName] => Arun
[id] => 2
[userId] => 1
[planName] => Coll Plan
)
)
)
Updated Expected Answer
Array
(
[0] => Array
(
[userId] => 1
[fullName] => Arun
[planId] => 2
[planName] => Cool Plan
[contactCount] => 2
)
[1] => Array
(
[userId] => 2
[fullName] => Sarvan
[planId] => 3
[planName] => Free Pla1
[contactCount] => 1
)
)
You can get the latest plan with a simple subquery, no need for grouping. The count of contacts can be done with a simple grouping:
SELECT u.id AS userId, u.fullName, p.id AS planId, p.planName, COUNT(c.userId) AS contactCount
FROM user_info u
LEFT JOIN user_active_plan p ON u.id = p.userId
LEFT JOIN contact_property c ON u.id = c.userId
WHERE p.id = (SELECT id
FROM user_active_plan
WHERE userId = u.id
ORDER BY id DESC
LIMIT 1)
GROUP BY c.userId;
You can also move the condition from the WHERE clause to the join:
SELECT u.id AS userId, u.fullName, p.id AS planId, p.planName, COUNT(c.userId) AS contactCount
FROM user_info u
LEFT JOIN user_active_plan p ON u.id = p.userId
AND p.id = (SELECT id
FROM user_active_plan
WHERE userId = u.id
ORDER BY id DESC
LIMIT 1)
LEFT JOIN contact_property c ON u.id = c.userId
GROUP BY c.userId;
I am trying to get row counts returned for different tables based on user_id value. users is a table of all users with a unique column of user_id. All other tables have a corresponding user_id column to join on it with.
I would think this would be fairly easy, but for some reason I cannot get the counts to return right.
What I want to accomplish is alerts = ? and locations = ? where ? is the total number of rows in that table where user_id = 1,2,3,4,5,6,7, or 8.
$stmt = $db->prepare("
SELECT
count(t_alerts.user_id) as alerts,
count(t_locations.user_id) as locations
FROM users
LEFT JOIN
(SELECT user_id
FROM alert_logs
WHERE alert_logs.event_title LIKE '%blocked%'
) as t_alerts
on t_alerts.user_id = users.user_id
LEFT JOIN
(SELECT user_id
FROM location_logs
) as t_locations
on t_locations.user_id = users.user_id
WHERE users.user_id IN(1,2,3,4,5,6,7,8)
");
$stmt->execute();
//get results
$results = $stmt->fetch(PDO::FETCH_ASSOC);
EDIT :
A bit of a modification to eliminate the need of supplying the IN values... I use this in some other queries to only get results for 'active' users...
$stmt = $db->prepare("
SELECT
(SELECT COUNT(*)
FROM alert_logs al
WHERE event_title LIKE '%blocked%' AND al.user_id = u.user_id
) as alerts,
(SELECT COUNT(*)
FROM location_logs ll
WHERE ll.user_id = u.user_id
) as locations
FROM
( SELECT account_id, computer_id
FROM computers
WHERE account_id = :account_id
ORDER BY computer_id ASC LIMIT 0, :licenses
) as c
INNER JOIN users as u
on u.computer_id = c.computer_id
");
$binding = array(
'account_id' => $_SESSION['user']['account_id'],
'licenses' => $_SESSION['user']['licenses']
);
$stmt->execute($binding);
I am running into the problem mentioned below with this statement... it is returning an array of counts per user rather than all counts combined into one result.
Array
(
[0] => Array
(
[alerts] => 6
[locations] => 4
)
[1] => Array
(
[alerts] => 3
[locations] => 5
)
[2] => Array
(
[alerts] => 1
[locations] => 4
)
[3] => Array
(
[alerts] => 0
[locations] => 0
)
[4] => Array
(
[alerts] => 0
[locations] => 0
)
[5] => Array
(
[alerts] => 0
[locations] => 0
)
[6] => Array
(
[alerts] => 0
[locations] => 0
)
[7] => Array
(
[alerts] => 0
[locations] => 0
)
)
What can I do to 'combine' results?
The problem is that the alerts are multiplying with the locations. So, if there are 10 alerts and 5 locations, the result is 50 rows. That is what gets counted.
The easy solution is to use count(distinct):
SELECT
count(distinct t_alerts.user_id) as alerts,
count(distinct t_locations.user_id) as locations
. . .
The better solution is often to use a subquery to do the counting along each dimension, and then join the results together.
EDIT:
In your case, nested subqueries in the select might be the best approach, because the query filters on users:
SELECT (SELECT COUNT(*)
FROM alert_logs al
WHERE event_title LIKE '%blocked%' AND
al.user_id = u.user_id
) as alerts,
(SELECT COUNT(*)
FROM location_logs ll
WHERE ll.user_id = u.user_id
) as locations
FROM users u
WHERE u.user_id IN (1,2,3,4,5,6,7,8)
EDIT II:
I see, there is no group by at the end of your query. In that case, you might as well do:
SELECT (SELECT COUNT(*)
FROM alert_logs al
WHERE event_title LIKE '%blocked%' AND
al.user_id IN (1,2,3,4,5,6,7,8)
) as alerts,
(SELECT COUNT(*)
FROM location_logs ll
WHERE ll.user_id IN (1,2,3,4,5,6,7,8)
) as locations;
You don't need the users table at all.
$options = array('order' => array('MemberMessage.send_date_time' => 'desc'),
'fields' => array('MemberMessage.id','MemberMessage.message'),
'group' => 'MemberMessage.message_reciever_id');
$this->MemberMessage->recursive = 0;
$MemberMessages = $this->MemberMessage->find('all',$options);
I think this should be enough?
$lastMessage = $this->Message->find('first', array(
'order' => array('Message.created' => 'desc')
));
Good luck!
i solve this by using this query:
$MemberMessages = $this->MemberMessage->query("SELECT *
FROM (
SELECT conversation_user_id,message, send_date_time
FROM (
SELECT DISTINCT message_reciever_id AS conversation_user_id, id, message, send_date_time
FROM (
SELECT *
FROM member_messages
WHERE message_sender_id =".$this->request->data['MemberMessage']['user_id'].
" ORDER BY send_date_time DESC
) AS t
GROUP BY message_reciever_id
UNION
SELECT DISTINCT message_sender_id AS conversation_user_id, id, message, send_date_time
FROM (
SELECT *
FROM member_messages
WHERE message_reciever_id =".$this->request->data['MemberMessage']['user_id'].
" ORDER BY send_date_time DESC
) AS u
GROUP BY message_sender_id
) AS CONVERSATION
ORDER BY send_date_time DESC
) AS conversation
GROUP BY conversation_user_id");
I have a weird problem with mysql_fetch_array( $result , MYSQL_BOTH ). This is how the problem is reproduced:
Do a query string that uses LEFT JOIN, GROUP BY
select *,
(A1.total - A2.borrow_total) as remain_total
from ( select Min(sample_created_date) as created_date,
sample_model,
sample_ncc,
count(*) as total ,
sample_sort
from tb_sample
where 1=1
AND sample_kho IN ( 'hanoi','tm_hanoi' )
group by sample_model, sample_sort, sample_ncc
) A1 left join
( select sample_model as sample_model1,
count(*) as borrow_total,
sample_ncc
from tb_sample B1 left join tb_sample_ticket B2
on B1.sample_borrow_status = B2.ticket_id
where 1=1
and ticket_bo_phan_duyet = 'finish'
and ticket_status_duyet = '1'
AND sample_kho IN ( 'hanoi','tm_hanoi' )
group by sample_model, sample_ncc
) A2 on A1.sample_model = A2.sample_model1
AND A1.sample_ncc = A2.sample_ncc left join tb_product_sort A3
on A1.sample_sort = A3.sort_code
where 1=1
order by created_date DESC
LIMIT 0, 30
Implement mysql_fetch_array( $result , MYSQL_BOTH ):
$rw = mysql_fetch_array( $result , MYSQL_BOTH );
Display value of $rw:
echo $rw['sample_cc'];
But there is nothing. Then I try print_r( $rw ), see below for the result:
Array
(
[0] => 2013-06-13 04:10:39
[created_date] => 2013-06-13 04:10:39
[1] => 3G
[sample_model] => 3G
**[2] => Gmobile
[sample_ncc] =>**
[3] => 1
[total] => 1
[4] => SIM
[sample_sort] => SIM
);
I see that $Rw[2] = 3G but $rw['sample_ncc'] is null.
This is a weird problem. Please help me solve it.
Try to give explicit aliases for sample_ncc columns in subqueries A1 and A2 (e.g. sample_ncc_a1 and sample_ncc_a2) or return only one that is on the left side of LEFT JOIN (meaning in A1).
I was given the task of translating an old query into Zend and all was going well until I went to create the union. I cannot post the actual contents of the query due to company regulations but basically what I have is:
$sql1 = $db->select()
->from(array('t' => 'tableOne'), array('t.id'))
->joinLeft(array('tt' => 'tableTwo'), 'tt.fk_tableOne_id = t.id', array())
->where($db->quoteInto('tt.active = ?', 'Y'));
$sql2 = $db->select()
->from(array('t' => 'tableOne'), array('t.id'))
->joinLeft(array('tt' => 'tableTwo'), 'tt.fk_tableOne_id = t.id', array())
->where($db->quoteInto('tt.active = ?', 'Y'));
$select = $db->select()->union(array($sql1, $sql2))->order('t.id');
Now, if I do a fetchAll on $sql1, it works. If I do a fetchAll on $sql2, it works. However, when I do a fetchAll on $select I get an error 1064 Syntax Error.
The sql string echoed by echo $select is basically
(ORDER BY `t.id` ASC) UNION (SELECT ... ) UNION (SELECT ...)
With the syntax error near ORDER BY ...
It seems like this should all be working since the two queries work independently, any ideas?
I tried a slightly modified query from yours:
$sql1 = $zdb->select()
->from(array('t' => 'articles'), array('t.id'))
->joinLeft(array('tt' => 'users'), 'tt.id = t.author_id', array())
->where($zdb->quoteInto('tt.level = ?', 'editor'));
$sql2 = $zdb->select()
->from(array('t' => 'blogs'), array('t.id'))
->joinLeft(array('tt' => 'users'), 'tt.id = t.updated_by', array())
->where($zdb->quoteInto('tt.level = ?', 'editor'));
$select = $zdb->select()->union(array($sql1, $sql2))->order('id');
echo $select;
and got the following:
SELECT `t`.`id` FROM `articles` AS `t` LEFT JOIN `users` AS `tt` ON tt.id = t.author_id
WHERE (tt.level = 'editor') UNION SELECT `t`.`id` FROM `blogs` AS `t` LEFT JOIN `users`
AS `tt` ON tt.id = t.updated_by WHERE (tt.level = 'editor') ORDER BY `id` ASC
What version of the framework do you have?