从sql查询中的另一个表添加计数

时间:2016-02-04 05:39:06

标签: mysql join

我已经使用连接创建了一个mysql查询我正在获取与其他人聊天的朋友列表。此查询包含三个表。现在想要从第4个表中添加一个包含用户聊天的计数。我在该表中有一个列名称readstatus,我在其中检查是否读取了消息。现在我想在这个查询中添加该表的计数,但我没有得到正确的方法在这里添加这个是查询

SELECT c.c_id, u.first_name, u.last_name, u.user_name, u.online_status, c.user_one, c.user_two, u.user_img, c.property_id, p.locality, p.city 
FROM  cpo_conversation c, 
      property_register u,  
      property_for_sale p 
WHERE CASE WHEN c.user_one = '19' THEN c.user_two = u.id 
           WHEN c.user_two = '19' THEN c.user_one = u.id END  
      (c.user_one = '19' OR c.user_two = '19') AND p.id = c.property_id 
ORDER BY c.c_id DESC

1 个答案:

答案 0 :(得分:0)

    SELECT (SELECT COUNT(*) from ***4th_table*** WHERE ***user_condition*** AND readstatus=1) as your_count, c.c_id, u.first_name, u.last_name, 
u.user_name, u.online_status, c.user_one, c.user_two, u.user_img, 
c.property_id, p.locality, p.city FROM cpo_conversation c, property_register 
u, property_for_sale p WHERE CASE WHEN c.user_one = '19' THEN c.user_two = 
u.id WHEN c.user_two = '19' THEN c.user_one = u.id END (c.user_one = '19' OR 
c.user_two = '19') AND p.id = c.property_id ORDER BY c.c_id DESC