Hey I’ll paypal someone $20 if they can help me figure out this query
Here are the important parts of my schema:
polls === id user_id title users === id name votes ==== id poll_id user_id timestamp friends ( pivot table, both below are user id's ) ==== user_id friend_id
I need a query that will do the following:
get poll title, user name of voter, user name of poll creator from polls where users i am friends with voted, organized by vote timestamp. Thanks
is a good site for quickly playing around with something like this if you’re so inclined.
sqlfiddle code:
create table polls (id int(11), user_id int(11), title varchar(20)); insert into polls (id, user_id, title) VALUES (1, 1, ‘poll number 1’); insert into users (id, name) values (1, ‘user 1’); insert into votes (id, poll_id, user_id, timestamp) values (1, 1, 1, 111); insert into friends(user_id, friend_id) values (2, 1); |
Query (assuming we’re running the query for user_id 2)
SELECT |
Pay me!
Results:
+ID—-TITLE————POLLCREATORID—-VOTERUSERID—-VOTERNAME—-POLLCREATORNAME+
————————————————————————————————|
|1—–poll number 1—–1——————1—————-user 1———user 1————–|
|2—–poll number 2—–1——————1—————-user 1———user 1————–|
|3—–poll number 3—–1——————1—————-user 1———user 1————–|
————————————————————————————————