Skip to content
Advertisement

Multi-parameter search with mysql and node.js

Let me preface by saying I’m very new to SQL (and back end design) in general. So for those annoyed with noob questions, please be gentle.

BACKGROUND:

I’m trying to build a product test database (storing test data for all our products) where I want a user to be able to refine a search to find test data they actually want. For example, they may start by searching for all products of a certain brand name, and then refine it with a product type, and/or refine it with a date range of when the test was done.

PROBLEM:

I’m having a hard time finding information on how to implement multi-parameter searches with mysql and node.js. I know you can do nested queries and joins and such within pure SQL syntax, but it’s not abundantly clear to me how I would do this from node.js, especially when certain search criteria aren’t guaranteed to be used.

Ex:

I know how to pass data from node.js to this procedure, but what if the user didn’t specify a product type? Is there a way to nullify this part of the query? Something like: AND product_type = ALL;

WHAT I’VE TRIED:

I’ve also looked into nesting multiple SQL procedures, but passing in dynamic data to the “FROM” clause doesn’t seem to be possible. Ex: if I had a brandname procedure, and a product type procedure, I don’t know how/if I can pass the results from one procedure to the “FROM” clause of the other to actually refine the search.

One idea was to create tables with the results in each of these procedures, and pass those new table names to subsequent procedures, but that strikes me as an inefficient way to do this (Am I wrong? Is this a completely legit way to do this?).

I’m also looking into building a query string on the node side that would intelligently decide what search criteria have been specified by the front end, and figure out where to put SQL AND’s and JOIN’s and what-nots. The example below actually works, but this seems like it could get ugly quick as I add more search criteria, along with JOINS to other tables.

CONCISE QUESTIONS:

  • Can I build 1 SQL procedure with all my search criteria as variables, and nullify those variables from node.js if certain criteria aren’t used?
  • Is there way to nest multiple MySQL procedures so I can pick the procedures applicable to the search criteria?
    • Is creating tables of results in a procedure, and passing those new table names to other procedures a reasonable way to do that?
  • Building the query from scratch in node is working, but it seems bloated. Is there a better way to do this?
  • Googling “multi-parameter search mysql nodejs” is not producing useful results for my question, i.e. I’m not asking the right question. What is the right question? What do I need to be researching?

Advertisement

Answer

One option is to use coalesce():

It may be more efficient do explicit null checks on the parameters:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement