Tenho três usuários de tabelas, favoritos e amigos.
usuários: id, nome, email
favoritos: id, favorite_id, user_id
amigos: id, friend_id, user_id
Consulta 1: juntando usuários de tabelas e amigos
select
friends.`friend_id`
from users
left join friends on users.id = friends.`friend_id`
where users.id != 18
and (user_id = 18 or user_id is NULL)
order by id asc
Consulta 2: Unindo tabelas de usuários e favoritos
select
users.`created_at`
, users.name
, users.email
, users.`image`
, users.is_online
, favorites.`favorite_id`
, users.id
, user_id
from users
left join favorites on users.id = favorites.`favorite_id`
where users.id != 18
and (user_id = 18 or user_id is NULL)
order by id asc
Ambas as consultas acima estão funcionando bem,
Agora, o que quero fazer é juntar as duas consultas, tentei algo assim,
Consulta tentada:
select
users.`created_at`
, users.name
, users.email
, users.`image`
, users.is_online
, favorites.`favorite_id`
, users.id
, user_id
, (
select
friends.`friend_id`
from users
left join friends on users.id = friends.`friend_id`
where users.id != $user_id
and (user_id = $user_id or user_id is NULL)
)
from users
left join favorites on users.id = favorites.`favorite_id`
where users.id != $user_id
and (user_id = $user_id or user_id is NULL)
order by id asc
Mas, não está mostrando dados, onde estou faltando. Por favor, me ajude.
saída da Consulta 1:
{
"data": [
{
"friend_id": 2
}
],
Resultado da consulta 2
"data1": [
{
"created_at": "2020-08-04 23:53:53",
"name": "yashyashwi",
"email": "[email protected]",
"image": "1596540233.jpeg",
"is_online": 0,
"favorite_id": null,
"id": 1,
"user_id": null
}
],
Saída desejada:
"datas": [
{
"created_at": "2020-08-04 23:53:53",
"name": "yashyashwi",
"email": "[email protected]",
"image": "1596540233.jpeg",
"is_online": 0,
"favorite_id": null,
"friend_id": 2,
"id": 1,
"user_id": null
}
],
Você deve fornecer dados de entrada . Eu os reconstruí. Verifique a lista começando no topo, com a WITH
palavra - chave.
CREATE TABLE users(
id INTEGER
, created_at TIMESTAMP
, name VARCHAR(32)
, email VARCHAR(32)
, image VARCHAR(32)
, is_online BOOLEAN
);
INSERT INTO users VALUES (
1
, TIMESTAMP '2020-03-24 05:35:45'
, 'yashyashwi'
, '[email protected]'
, '1596540233.jpeg'
, FALSE
);
CREATE TABLE favorites (
id INTEGER
, favorite_id INTEGER
, user_id INTEGER
);
INSERT INTO favorites VALUES(NULL,NULL,NULL);
CREATE TABLE friends (
id INTEGER
, friend_id INTEGER
, user_id INTEGER
);
INSERT INTO friends VALUES (1,2,1);
select
users.created_at
, users.name
, users.email
, users.image
, users.is_online
, favorites.favorite_id
, friends.friend_id
, users.id
, favorites.user_id
from users
left join favorites on users.id = favorites.favorite_id
left join friends on users.id=friends.id
where users.id != 18
and (favorites.user_id = 18 or favorites.user_id is NULL)
;
-- out created_at | name | email | image | is_online | favorite_id | friend_id | id | user_id
-- out ------------+------------+----------------------+-----------------+-----------+-------------+-----------+----+---------
-- out 2020-03-24 | yashyashwi | [email protected] | 1596540233.jpeg | f | (NULL) | 2 | 1 | (NULL)
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras