php - Filter MySQL records between two tables by Join depending on a specific column value -
recently i've been working on php/mysql script reads information database user info , file info stored in 2 seperate tables.
below schema's tables.
table1
uid | username | permissionlevel 1 | first | 1 2 | next | 3 3 | more | 2
table2
fid | filename | filelevel | uploadusername 1 | file.txt | 2 | first 2 | hand.mp4 | 1 | first 3 | 1245.dds | 1 | next 4 | beta.sql | 3 | more
for purpose of message have omitted passwords column , file title/description column, since play no part in result trying achieve.
so far have come sql code
select distinct table2.*, table1.* table2 join table2 on table2.filelevel <= table1.permissionlevel table2.uploadusername = table1.username order fid desc limit 7
this generates appropriate listing want, not filter level of content shown.
any user permissionlevel of 1 should see files filelevel of 1. users permissionlevel of "2" can see files of filelevel of both 2 , 1, , on.
but @ current stage seems want display results regardless of file/permission level.
i've been stuck @ issue couple of days , can't seem head around this. it's simple may have overlooked, hope fresh pair of eyes may me.
my in advance.
i'm not sure if understand question fully, make of it. have query returns uploaded files. want filter list, shows less results, depending on user's permission level. please note user active user on website, not same user uploaded file, condition not work on table1
table of query.
a common solution have session variable storing current user's id, , possibly other information.
$_session['user'] = 'somebody'; $_session['permissionlevel'] = 3;
if don't have permissionlevel
in local variable, have join on table1
table twice, once find uploader , once find permission level of current user. have typo in original query. following give both users
select * table2 join table1 uploader table2.uploadusername=table1.username join table1 currentuser table2.persmissionlevel<=table1.permissionslevel , users.username='$_session[user]' order fid desc
Comments
Post a Comment