Doubt in query mount with INNER JOIN

Asked

Viewed 175 times

0

I have the following query:

$conexao = connect();

$consulta3 = mysql_query("SELECT meu.id, f.idfollowed, eu_sigo.fname, eu_sigo.id, eu_sigo.profile, eu_sigo.photoperf, p.id, p.title, p.link, p.description, p.descr, p.url, p.photo, p.visivel, p.type, p.description, p.data
FROM u636623377_users AS meu
INNER JOIN u636623377_follows AS f ON f.idfollower = meu.id
INNER JOIN u636623377_users AS eu_sigo ON f.idfollowed = eu_sigo.id
INNER JOIN u636623377_posts AS p ON p.userid = f.idfollowed
WHERE meu.id = '$idperf'"); # id do usuário logado     

This query takes all the posts from the users I follow, along with some more information about them, such as their name, photo, and profile link that is stored in another table. This is working perfectly. However, it returns only the posts of the users I follow. I would like her to return my posts as well. What should I implement for this to happen?

Table structure:

u636623377_follows = pastebin. com/i4QfJBBH  //junte os espaços

u636623377_posts = pastebin. com/tMUB5t5E    //junte os espaços

u636623377_users = pastebin. com/sabLHx97    //junte os espaços
  • 1

    Do not use pastbin, use stackoverflow markup, friend, PLEASE READ: http://answall.com/help/mcve grateful :)

2 answers

1

Your answer is in UNION. Briefly you can insert other queries in the return but they should return the same number of columns being of the same type.

Concatenate the following passage:

UNION    

SELECT meu.id    
     , NULL -- f.idfollowed    
     , NULL -- eu_sigo.fname    
     , NULL -- eu_sigo.id    
     , NULL -- eu_sigo.profile    
     , NULL -- eu_sigo.photoperf    
     , p.id    
     , p.title    
     , p.link    
     , p.description    
     , p.descr    
     , p.url    
     , p.photo    
     , p.visivel    
     , p.type    
     , p.description    
     , p.data    
FROM u636623377_users AS meu    
INNER JOIN u636623377_posts AS p ON p.userid = meu.id    
WHERE meu.id = '$idperf'    

Since they are your posts, it makes no sense to return information from followers and, for this reason, we insert a neutral value in these columns, in this case NULL.

UPDATE

I left comments next to the Nulls to be visible the columns we are dealing with. If you want to remove them, feel free.

-1

Try to include another Join in the posts table to pick up your posts,

$conexao = connect();

$consulta3 = mysql_query("SELECT meu.id, f.idfollowed, eu_sigo.fname, eu_sigo.id, eu_sigo.profile, eu_sigo.photoperf, p.id, p.title, p.link, p.description, p.descr, p.url, p.photo, p.visivel, p.type, p.description, p.data
FROM u636623377_users AS meu
INNER JOIN u636623377_follows AS f ON f.idfollower = meu.id
INNER JOIN u636623377_users AS eu_sigo ON f.idfollowed = eu_sigo.id
INNER JOIN u636623377_posts AS p ON p.userid = f.idfollowed
INNER JOIN u636623377_posts AS meusposts ON meusposts.userid = meu.id
WHERE meu.id = '$idperf' ORDER BY p.data DESC"); # id do usuário logado     

Browser other questions tagged

You are not signed in. Login or sign up in order to post.