This is an “example version” of my current SQL query:
SELECT DISTINCT files.* FROM vw_files files
LEFT JOIN tbl_file_ext fext
ON files.id = fext.fileID
INNER JOIN tbl_extensions ext ON fext.extensionID = ext.id
WHERE ext.extension = 'exe' OR ext.extension = 'com'
The WHERE clause is driven by a user entering a comma delimited string.
What I’d like is to order/group by the input so say if a user enters:
com, exe, jpg the WHERE clause will be:
WHERE ext.extension = 'com' OR ext.extension = 'exe' OR ext.extension = 'jpg'
The results need to be ordered by priority of the inputs something like:
File1 com exe jpg
File2 com exe jpg
File3 com exe
File4 com jpg
File5 com
The WHERE clause is constructed via PHP, but how can I construct a ORDER or GROUP clause which will retuern results ordered by priority of the usrs inputs.
Edit as requested —-
The above was just an example as I stated, and didn’t feel appropriate as i’m doing something similar to stackoverflow. However, the real setup is as follows:
tbl_solutions –
id, authorID, ipAddress, title, question, dateAdded, locked, active
tbl_solution_tags –
id, FK solutionID, FK tagID
tbl_solution_files –
id, FK solutionID, FK fileID
vw_solution_and_file –
username, id (solution id), authorID, ipAddress, title, question, dateAdded, locked, active, fileName, fileSize, removed, public
select sol.*, files.fileName, files.fileSize, files.removed, files.public from
tbl_solutions sol left join
tbl_files files on (select sf.fileID from tbl_solution_files sf
where sf.solutionID = sol.id) = files.id;
vw_solution_file_tags – same as above but also returns tags
SELECT sf.*,GROUP_CONCAT(tags.tag SEPARATOR ', ') as 'tags' FROM
vw_solution_and_file sf LEFT JOIN tbl_solution_tags st
ON sf.id = st.solutionID INNER JOIN tbl_tags tags
ON st.tagID = tags.id GROUP BY sf.id
Example output from vw_solution_file_tags:
username id authorID ipAddress title question dateAdded locked active fileName fileSize removed public tags
craig 24 81 127.0.0.1 “A Title” “A question” 2012-01-01 0 1 NULL NULL NULL 1 c++, ASP, gdb
craig 25 81 127.0.0.1 “B Title” “B question” 2012-01-01 0 1 NULL NULL NULL 1 c++, ASP, Windows
So at the moment all I’m doing is using vw_solution_file_tags in my page, however the user is going to be able to apply a filter on the tags. So he may say ASP, Windows so both results above will show. But they need to be in order of precedence of the users input.
So the code I want to use in my PHP Query will be similar to vw_solution_file_tags but only where the tags match user input and ordered by that input.
I think you may have to construct the
ORDER BYfrom PHP code also. Try something like the follwoing. Note: first do it directly on MYSQL, if it works then move it into PHP.