Stooney Posted April 24, 2009 Share Posted April 24, 2009 This is tough to explain. I am trying to squeeze two things into one query. Originally the query simply did a search for files based a criteria, easy. What I'm trying to integrate into the same query is to check whether or not the file_id of each file that fits the criteria is entered in another table (highlights) and associated with the users' user_id. So the highlights table looks like this: highlight_id - user_id - file_id The highlights table just records when a user downloads a file. The whole point of this query is to search files based on the criteria (working already) and set the outputs tr (table row) to the correct class (highlight or no highlight). So in the end all this query is for is to search for files and know when to highlight or not hightlight a file. A file that has not been downloaded (not yet in the highlights table) should be highlighted. Here is what I have so far, the IF statement isn't all there because I know it's wrong. The necessary part is there though. <?php $query="SELECT f.file_id, f.file_size, f.file_dname, CONCAT(u.user_fname, ', ', u.user_lname) as tname, CONCAT(c.client_fname, ', ', c.client_lname) as cname, IF((**STUCK HERE**)>0, 1, 0) as hl FROM ".CLIENT_FILES." as f, ".USERS." as u, ".CLIENTS." as c WHERE ".$q." AND f.client_id=c.client_id AND u.user_id=c.user_id ORDER BY f.file_dateadded DESC"; ?> Hope I made sense, it's late Thank You. EDIT: MySQL 5 Quote Link to comment https://forums.phpfreaks.com/topic/155476-if-statement-in-query/ Share on other sites More sharing options...
fenway Posted April 24, 2009 Share Posted April 24, 2009 Switch to proper JOINs... then LEFT JOIN to the highlight table on ( user_id ), and use IFNULL() to check. Quote Link to comment https://forums.phpfreaks.com/topic/155476-if-statement-in-query/#findComment-818281 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.