Jump to content

NOT EXISTS, RAND(), and JOIN


Go to solution Solved by Barand,

Recommended Posts

I'm stumped on how to get this join to work:

Statement:

SELECT * FROM votes WHERE NOT EXISTS (`votes`.`poll_id` = `user_votes`.`vote_poll_id` AND `user_votes`.`user_id`='1') limit 1 order by rand()

 

Error:

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 '`votes`.`poll_id` = `user_votes`.`vote_poll_id`) limit 1 order by rand()' at line 3

 

Possibly, but doubt it:

the table is completely blank.

 

Goal:

Find all the entries that the user hasn't voted on as of yet, then submit them randomly to the user.

Link to comment
https://forums.phpfreaks.com/topic/278618-not-exists-rand-and-join/
Share on other sites

To do this you need 3 tables

 

voter : voterid | voter

item : itemid | item

votes : itemid | voterid

 

Create a cartesian join between voter and item to get all combinations then left join with votes to see which combinations are missing

I pull the data based upon the user_votes table, which tells me what database to query, because I have a byte operator in the user_votes table tied into a switch statement.  The data displays properly.

 

The elimination tables I'm having trouble with are:

user_votes //this table

s_fulltext.png vote_poll_id user_id Vote

 

and

 

votes //eliminates options from this table

s_fulltext.png poll_id user_id type item_id yes no

 

by

`votes`.`poll_id` = `user_votes`.`vote_poll_id` AND `user_votes`.`user_id`='$user'

 

Problem:

the elimination table works fine with the data when it's matched up, but not telling me the options that aren't matching up, if there are any.

One more little tweak issue:

The pull wasn't pulling the vote based upon unique users

 

SELECT v.* FROM votes v LEFT JOIN user_votes uv ON v.poll_id = uv.vote_poll_id WHERE

v.user_id != $id // the one who owns the item being voted on (working)

AND

uv.voter_id = $id // unique voter that is not the owner (stops all outputs)

AND uv.vote_poll_id IS NULL ORDER BY RAND() LIMIT 1

 

Ignore the comments, as they're just footnotes for troubleshooting help.

I renamed the column name to "voter_id", because it appeared to be used a second time in the other table.

Edited by Q695
  • Solution

"WHERE" selection conditions on a left-joined table need to be part of the join.

 

Try

SELECT v.* 
FROM votes v 
    LEFT JOIN user_votes uv 
        ON v.poll_id = uv.vote_poll_id 
        AND uv.voter_id = $id
WHERE
    v.user_id != $id
    AND uv.vote_poll_id IS NULL 
ORDER BY RAND() 
LIMIT 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.