Saturday, 10 August 2013

Mysql two tables, select all values from one table, compare if exists and then return One random value

Mysql two tables, select all values from one table, compare if exists and
then return One random value

I'm new here as for posting questions, but I've been looking for answers
since long time ago. Still what I'm trying to do now I didn't found for
the right solution or I just didn't know how to use it if I did found it.
Here's my sql query:
$sql = "SELECT * FROM table_links AS link_id WHERE link_points > '1'
AND NOT EXISTS (SELECT * FROM table_surf AS slink_id WHERE user_id = '$user'
AND surf_time = '$time') ORDER by RAND() LIMIT 1";
I'm trying to select a random LINK id (wich has points) and check if that
link was allready shown to a user today by looking into the 2nd table
where all LINK ID's are saved as SLINK ID's with user ID's and time of the
last visits.
If last visit was today ($time) then it should not return anything.
I'm a newbie to php/mysql but I'm trying to learn, still this is a
situation where I'm not sure if it is even possible to realise a complex
query like this one.

No comments:

Post a Comment