Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2012, 03:10 AM   PM User | #1
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
I'm struggling with multiple INNER JOINs

I used to have 3 tables: users, polls, and choices. I used one INNER JOIN query to get all choices by a specific user ID, and then I matched the poll.id with choice.pollid. I got that to work and I was happy with it, but now I'm implementing a 4th table, votes, where I will store vote id, choice id, IP, and (if logged in), user id.

My initial query:
PHP Code:
$qry 'SELECT p.id, p.question, p.date, p.active, c.choice, c.votecount, c.pollid, c.id
FROM polls p
INNER JOIN choices c ON c.pollid = p.id
WHERE p.userid = ' 
$currentid '
ORDER BY p.date DESC'

I slightly modified it but it doesn't work, and I can't find the syntax for multiple joins.
PHP Code:
SELECT p.idp.questionp.datep.activec.choicec.votecountc.pollidc.idv.id
FROM polls p
INNER JOIN choices c ON c
.pollid p.id
WHERE p
.userid 1000
INNER JOIN votes v ON v
.pollid p.id
WHERE p
.id 1
ORDER BY p
.date DESC 
In essence, I want to pull every vote on every poll by a specific userid. And then I'll probably store that data in multi-dimensional arrays and then reference it later (?).
KULP is offline   Reply With Quote
Old 12-07-2012, 03:22 AM   PM User | #2
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
I followed instructions (here) to use parentheses and still.. nothing. I thought about taking out the ONs but I don't know which one to remove and how it would function. Hmm...

PHP Code:
SELECT p.idp.questionp.datep.activec.choicec.votecountc.pollidc.idv.idv.ipaddress
FROM 
(polls p
INNER JOIN choices c WHERE c
.pollid p.id
ON p
.userid 1000
INNER JOIN votes v WHERE v.pollid p.id
ON p
.id 1
ORDER BY p
.date DESC 
Error:
Code:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE c.pollid = p.id ON p.userid = 1000) INNER JOIN votes v WHERE v.pollid =' at line 3

Last edited by KULP; 12-07-2012 at 03:27 AM..
KULP is offline   Reply With Quote
Old 12-07-2012, 12:07 PM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Error: WHERE is illegal in this place. ON is the WHERE of a JOIN.

I’d try
Code:
SELECT
    ...
FROM polls p
-- begin of join
INNER JOIN choices c
    ON c.pollid = p.id
INNER JOIN votes v
    ON v.pollid = p.id
-- end of join
WHERE -- now the condition applied to the compiled table
    p.userid = ? -- or whatever holds the user ID
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 12-07-2012, 02:39 PM   PM User | #4
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
Dang, I tried that before but then found something that said ON and WHERE were the same thing. I changed that and then changed the statement at the end and now it works!

Now I just have to decide, if I have a vote table, if I should take the votecount column out of the choices table and just use php to manually add up votes or if I should keep it there for easy access. Hmm...

PHP Code:
SELECT p.idp.questionp.datep.activec.choicec.votecountc.pollidc.idv.idv.ipaddress 
FROM 
(polls p 
INNER JOIN choices c ON c
.pollid p.id)  
INNER JOIN votes v ON v.choiceid c.id 
WHERE p
.userid 1000
ORDER BY p
.date DESC 
EDIT: Would it be bad if I had two queries in one page? I'm thinking of having one query pull the polls / choices (as I already have set up) and then another pull the votes and tally them up for me.

Last edited by KULP; 12-07-2012 at 02:42 PM..
KULP is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:32 AM.


Advertisement
Log in to turn off these ads.