2012-05-03 17 views
5

ho questo schema POST WITH FEEDSPHP MySQL come rapporto tre tabelle che mostrano le uscite da tabelle diverse

Quello che voglio fare è avere questa uscita: Likes post - like Facebook

Come si riesce a fare la query di questo ?

Ho questo codice

SELECT users.firstname, users.lastname, 
     users.screenname, posts.post_id, posts.user_id, 
     posts.post, posts.upload_name, 
     posts.post_type, posts.date_posted 
FROM website.users users 
INNER JOIN website.posts posts ON (users.user_id = posts.user_id) 
ORDER BY posts.pid DESC 
//PROBLEM with this one is that it only views the post from all users. 

//SO I added 
SELECT COUNT(user_id) AS friends, SUM(user_id = ?) AS you, user_id 
FROM feeds WHERE post_id = ? 
//This one will give you two fields containing how many different users **feeds** the 
post 

Si prega di aiutare i ragazzi. In realtà questo sto seguendo solo lo di Facebook "LIKE" status l'unica cosa è che non sono un dilettante con questo genere di cose quindi sarei felice di sentire tutte le tue risposte. Ho davvero bisogno del vostro aiuto

risposta

3

Se ti ho capito bene, si desidera un outer join con la tabella feeds (al fine di mantenere tutte posts anche se non c'è sono associati feeds), poi GROUP BY post.pid al fine di amalgamare insieme tutto tali feed per ogni post e SELECT l'informazione desiderata.

io uso la funzione di MySQL GROUP_CONCAT() per ottenere un elenco separato da virgole di tutti gli utenti (fino a group_concat_max_len) che hanno un "feed" per il determinato posto (è possibile modificare il delimitatore con il modificatore SEPARATOR, se lo si desidera) .

SELECT users.firstname, users.lastname, 
     users.screenname, posts.post_id, posts.user_id, 
     posts.post, posts.upload_name, 
     posts.post_type, posts.date_posted, 
     COUNT(feeds.user_id) AS friends, -- number of "likes" 
     SUM(feeds.user_id = ?) AS you,  -- did I like this? 
     GROUP_CONCAT(feeds.user_id)  -- who likes it? 
FROM website.users users 
INNER JOIN website.posts posts ON (users.user_id = posts.user_id) 
LEFT JOIN website.feeds feeds ON (posts.post_id = feeds.post_id) 
GROUP BY posts.pid 
ORDER BY posts.pid DESC 

UPDATE

Per ottenere il nome completo di utenti che hanno "voluto", il post, escludendo se stessi, si ha la necessità di aderire al tavolo users una seconda volta:

SELECT users.firstname, users.lastname, 
     users.screenname, posts.post_id, posts.user_id, 
     posts.post, posts.upload_name, 
     posts.post_type, posts.date_posted, 
     COUNT(feeds.user_id) AS friends,      -- number of "likes" 
     SUM(feeds.user_id = ?) AS you,      -- did I like this? 
     GROUP_CONCAT(
     CASE WHEN NOT likes.user_id = ? THEN    -- exclude self 
      CONCAT_WS(' ', likes.firstname, likes.lastname) -- full names 
     END 
     ) 
FROM website.users users 
INNER JOIN website.posts posts ON (users.user_id = posts.user_id) 
LEFT JOIN website.feeds feeds ON (posts.post_id = feeds.post_id) 
LEFT JOIN website.users likes ON (feeds.user_id = likes.user_id) 
GROUP BY posts.pid 
ORDER BY posts.pid DESC 
+0

Ringrazia molto compagno! Ma fratello, su questa riga "SUM (user_id =?) COME tu, - mi è piaciuto questo? GROUP_CONCAT (user_id)" dovrebbe essere "feeds.user_id"? Ho un errore che dice: "Colonna user_id nella lista dei campi è ambigua –

+0

Sì, mi dispiace:' feeds.user_id'. Aggiornato – eggyal

+0

Grazie mille ragazzi per tutto il vostro aiuto! –

2

Se si desidera farlo per tutti gli utenti e contemporaneamente ottenere i feed, Si ha ve a partecipare a questo tavolo feed:

SELECT u.firstname, u.lastname, 
    u.screenname, p.post_id, p.user_id, 
    p.post, p.upload_name, 
    p.post_type, p.date_posted, 
    COUNT(f.user_id) AS friends, SUM(f.user_id = ?) AS you 
FROM website.users u 
INNER JOIN website.posts p ON (u.user_id = p.user_id) 
LEFT JOIN website.feeds f ON (p.post_id = f.post_id) 
GROUP BY p.pid 
ORDER BY p.pid DESC 

Questo dovrebbe fare il trucco ...

+0

Grazie amico! cercando di capire tutto –

+0

@PeterWateber Prego. – shadyyx