sql server - Selects from multiple tables for Activities feed -


i have social app trying create friend activities feed using azure sql server.

i have 3 tables want select from:

songs -createdat -id -userid -trackname -etc

comments -createdat -id -userid -songid -text

likes -createdat -id -userid -songid

i have users current user following stored in array named 'follows'.

how go selecting 40 recent items 3 tables userid in each table in follows array?

edit:

function getactivities(userid) {    var deferred = q.defer();    var follows = [];    getfollowing(userid).then(function (results) {       follows.push(userid);        _.each(results, function (user) {            follows.push(user.touserid);        });        return;     }).then(function () {        var stringified = "'" + follows.join("','") + "'";        var querystring = "select * comments, songs, likes comments.userid in (" + stringified + ") or songs.userid in (" + stringified +") or likes.userid in (" + stringified + ")";       var params = [];        return sqlquery(querystring, params);    }).then(function (results) {        console.log('activities: ', results);        deferred.resolve(results);    }, function (error) {        console.log('error: ', error.message);        deferred.reject(error.message);    });    return deferred.promise;  } 

alright, dug joins little more , realized how easy once wrap head around it. here did complete this:

var querystring = "select top 50 follows.id followid, follows.touserid followtouserid, follows.fromuserid followfromuserid, comments.text commenttext, profiles.userid, profiles.username, profiles.name, profiles.profileimage, songs.trackid, songs.trackname, songs.artistname, songs.collectionname, songs.artworkurl100, songs.caption, songs.id songid, activities.id activityid, activities.type activitytype, activities.objectid activityobjectid, activities.parenttype activityparenttype, activities.parentid activityparentid, activities.__createdat activitycreatedat activities ";  querystring += "inner join profiles on (profiles.userid = activities.userid) ";  querystring += "left join songs on (songs.id = activities.objectid , activities.type = 'songs') or (songs.id = activities.parentid , activities.parenttype = 'songs') ";  querystring += "left join comments on (activities.type = 'comments' , comments.id = activities.objectid) ";  querystring += "left join follows on (activities.type = 'followed' , activities.userid = follows.fromuserid) ";  querystring += "where activities.userid in (select follows.touserid userid follows follows.fromuserid = ? , follows.isfollowed = 'true') ";  querystring += "order activities.__createdat desc";  var params = [userid];  mssql.query(querystring, params, {      success: function (results) {          _.each(results, function (result) {               //remove columns null or undefined values              (var in result) {               if (result[i] === null || result[i] === undefined) {                 delete result[i];               }             }          });          response.send(200, results);      },      error: function (error) {         response.send(400, error.message);       }  }); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -