Мне дали задание по работе с приложением в чате. Там был список всех чатов пользователя. До сих пор у него было только 1 к 1 чату. Это была структура таблиц.
Чаты:
И сообщения
В настоящее время в нем были только одиночные чаты, поэтому chats.type ограничивался только одиночными.
Этот запрос я использовал
SELECT
*
FROM
(
SELECT
"" AS nickname,
chats.id AS chat_id,
chats.last_message_id,
chats.updated_at,
chats.type,
Unix_timestamp(chats.updated_at) AS unix_time,
users.firstname,
users.lastname,
users.profile_image,
messages.message,
messages.is_read,
messages.sender_id,
chats.sender_id AS user_id,
(
SELECT
Count(messages.id)
FROM
messages
WHERE
chats.id = messages.chat_id
AND receiver_id = 76
AND (is_read = 0 || is_read = 1)
) AS un_read_count
FROM
chats
JOIN users ON users.id = chats.sender_id
JOIN messages ON messages.id = chats.last_message_id
WHERE
chats.receiver_id = 76
AND chats.last_message_id != 3
UNION
SELECT
"" AS nickname,
chats.id AS chat_id,
chats.last_message_id,
chats.updated_at,
chats.type,
Unix_timestamp(chats.updated_at) AS unix_time,
users.firstname,
users.lastname,
users.profile_image,
messages.message,
messages.is_read,
messages.sender_id,
chats.receiver_id AS user_id,
(
SELECT
Count(messages.id)
FROM
messages
WHERE
chats.id = messages.chat_id
AND receiver_id = 76
AND (is_read = 0 || is_read = 1)
) AS un_read_count
FROM
chats
JOIN users ON users.id = chats.receiver_id
JOIN messages ON messages.id = chats.last_message_id
WHERE
chats.sender_id = 76
AND chats.last_message_id != 3
) AS r
ORDER BY
r.updated_at
Сейчас Я интегрировал групповые чаты тоже. Я добавил третий UNION, но я не понимаю, что в нем не будет chats.sender_id или chats.receiver_id, так как можно получить эту информацию на основе chat_members.user_id, чтобы я мог также просматривать групповые чаты. Любая помощь или направление высоко ценится. Спасибо.
Это таблица chat_members
Это то, что я пробовал до сих пор
SELECT
*
FROM
(
SELECT
"" AS nickname,
chats.id AS chat_id,
chats.last_message_id,
chats.updated_at,
chats.type,
Unix_timestamp(chats.updated_at) AS unix_time,
users.firstname,
users.lastname,
users.profile_image,
messages.message,
messages.is_read,
messages.sender_id,
chats.sender_id AS user_id,
(
SELECT
Count(messages.id)
FROM
messages
WHERE
chats.id = messages.chat_id
AND receiver_id = 76
AND (is_read = 0 || is_read = 1)
) AS un_read_count
FROM
chats
JOIN users ON users.id = chats.sender_id
JOIN messages ON messages.id = chats.last_message_id
WHERE
chats.receiver_id = 76
AND chats.last_message_id != 3
UNION
SELECT
"" AS nickname,
chats.id AS chat_id,
chats.last_message_id,
chats.updated_at,
chats.type,
Unix_timestamp(chats.updated_at) AS unix_time,
users.firstname,
users.lastname,
users.profile_image,
messages.message,
messages.is_read,
messages.sender_id,
chats.receiver_id AS user_id,
(
SELECT
Count(messages.id)
FROM
messages
WHERE
chats.id = messages.chat_id
AND receiver_id = 76
AND (is_read = 0 || is_read = 1)
) AS un_read_count
FROM
chats
JOIN users ON users.id = chats.receiver_id
JOIN messages ON messages.id = chats.last_message_id
WHERE
chats.sender_id = 76
AND chats.last_message_id != 3
UNION
SELECT
chats.group_name AS nickname,
chats.id AS chat_id,
chats.last_message_id,
chats.updated_at,
chats.type,
Unix_timestamp(chats.updated_at) AS unix_time,
"" AS users.firstname,
"" AS users.lastname,
"" AS users.profile_image,
messages.message,
"" AS messages.is_read,
messages.sender_id,
chats.receiver_id AS user_id,
(
SELECT
Count(messages.id)
FROM
messages
WHERE
chats.id = messages.chat_id
AND receiver_id = 76
AND (is_read = 0 || is_read = 1)
) AS un_read_count
FROM
chats
JOIN users ON users.id = chats.receiver_id
JOIN messages ON messages.id = chats.last_message_id
WHERE
chats.sender_id = 76
AND chats.last_message_id != 3
UNION
SELECT
chats.group_name AS nickname,
chats.id AS chat_id,
chats.last_message_id,
chats.updated_at,
Unix_timestamp(chats.updated_at) AS unix_time,
"" AS users.firstname,
"" AS users.lastname,
"" AS users.profile_image,
messages.message,
"" AS messages.is_read,
messages.sender_id,
'' AS user_id,
'' AS un_read_count
FROM
chats
JOIN users ON users.id = chats.receiver_id
JOIN messages ON messages.id = chats.last_message_id
WHERE
chats.sender_id = 76
AND chats.last_message_id != 3
) AS r
ORDER BY
r.updated_at