I want to assign an sql sting to a variable in Javascript. But I want to loop through an array to build par of the sql statement.
static sql:
var tag = [fun, beach, sun];
var sql = "SELECT * FROM myTable "
+"WHERE id > 5" //...
// loop through tag array and add to the sql
function tagQuery(tag) {
for (var i = 0; i < tag.length; i++) {
var tagQuery = "AND WHERE tags like %'"+tag[i]+"'%";
return tagQuery;
};
}
It is not clear to me how to get each result of the tagQuery function to be added to the sql sting?
EDIT: maybe a more clear way to ask the question is: How can I get each iteration of the tagQuery function to be joined with the sql variable?
LIKE, will find values oftagswhere all your values occur IN THE SAME STRING:tags LIKE '%fun%%beach%%sun%'. Is that really what you want? Or are you actually trying to find situations withtags LIKE '%fun%'andtags LIKE '%beach%'andtags LIKE '%sun%'?str += " OR tags like '%" + tag[i] + "%' ", within the function, then replacevar i = 0withvar i = 1(the beginning of the loop), then make the last two linessql += " AND tags like '%" + tag[0] + "%' "; sql += tagQuery(tag);tags, for example:sql += " AND (tags like '%" + tag[0] + "%' "; sql += tagQuery(tag) + ")";