在mysql中从另一个表添加连接数

时间:2016-02-09 08:19:52

标签: php mysql

我通过在MYSQL中连接三个表来获取一些记录。我得到的记录是正确的。但现在我必须在此查询中添加一个表,我必须为登录用户获取未读消息。 这是我正在使用的查询

SELECT c.c_id, u.id AS user_id, u.first_name, u.last_name, u.user_name, u.online_status, 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 AND (
c.user_one = '19'
OR c.user_two = '19'
)
AND p.id = c.property_id
ORDER BY c.c_id DESC 

这是我的第四张表po_conversation_reply

po_conversation_reply

我想为每个登录的用户添加此表的read_unread_status计数。我尝试了一些但是这给了我每个用户相同的结果

SELECT (

SELECT COUNT( * )
FROM cpo_conversation_reply
WHERE user_id_fk = u.id
AND read_unread_status =0
) 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 AND (
c.user_one = '19'
OR c.user_two = '19'
)
AND p.id = c.property_id
ORDER BY c.c_id DESC

0 个答案:

没有答案