Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL how to order based on user inputs with joined tables?
    primarykey
    data
    text
    <p>This is an "example version" of my current SQL query:</p> <pre class="lang-sql prettyprint-override"><code>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' </code></pre> <p>The <code>WHERE</code> clause is driven by a user entering a comma delimited string.</p> <p>What I'd like is to order/group by the input so say if a user enters:</p> <p><code>com, exe, jpg</code> the WHERE clause will be:</p> <pre class="lang-sql prettyprint-override"><code>WHERE ext.extension = 'com' OR ext.extension = 'exe' OR ext.extension = 'jpg' </code></pre> <p>The results need to be ordered by priority of the inputs something like:</p> <pre class="lang-none prettyprint-override"><code>File1 com exe jpg File2 com exe jpg File3 com exe File4 com jpg File5 com </code></pre> <p>The <code>WHERE</code> clause is constructed via PHP, but how can I construct a <code>ORDER</code> or <code>GROUP</code> clause which will retuern results ordered by priority of the usrs inputs.</p> <p>Edit as requested ---- </p> <p>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:</p> <p>tbl_solutions - id, authorID, ipAddress, title, question, dateAdded, locked, active</p> <p>tbl_solution_tags - id, FK solutionID, FK tagID</p> <p>tbl_solution_files - id, FK solutionID, FK fileID</p> <p>vw_solution_and_file - username, id (solution id), authorID, ipAddress, title, question, dateAdded, locked, active, fileName, fileSize, removed, public</p> <pre class="lang-sql prettyprint-override"><code>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; </code></pre> <p>vw_solution_file_tags - same as above but also returns tags</p> <pre class="lang-sql prettyprint-override"><code>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 </code></pre> <p>Example output from vw_solution_file_tags:</p> <p>username id authorID ipAddress title question dateAdded locked active fileName fileSize removed public tags </p> <p>craig 24 81 127.0.0.1 "A Title" "A question" 2012-01-01 0 1 NULL NULL NULL 1 c++, ASP, gdb </p> <p>craig 25 81 127.0.0.1 "B Title" "B question" 2012-01-01 0 1 NULL NULL NULL 1 c++, ASP, Windows</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload