简体   繁体   中英

Node JS select from MySQL filter array column by array object

I have a column whose contents can null, a single integer, or several integers separated by a comma. I want to search for specific rows by using my column to filter out unwanted data. Instead of a search string, I have an array that can have a single element or several, How can I work out on this? Am using Node JS + MySQL. The solution below only works when the column data is a single integer. How can I make it work when the column contains several integers too?

connection.query("SELECT * FROM `vw_tenants` WHERE units is not null AND property_code =? AND (floors_comunn IN (?);", [property_code, floors], (err, data) => {
connection.release();
if(err) {
    logger.error(err);
    result(null);
} else {
    result(data);
}

});

The " floors " variable can be [1] or have several elements ie [0,1,3,5,..n] The " floors_comunn " table column can contain null, single integer ie 3 , or a group of integers separated by a comma ie 2,4,5

Your question is not clear to me:(. But I can say one thing, you have to change the database design. It is not good practice to store multiple values in a single field. It should be atomic. This is the very first normal form (1NF) Normal forms

In DBMS there are many normal forms. It is always good practice to use at least upto 3rd normal form or BCNF.

MySQL also supports JSON Objects now. You can use JSON functions of mysql to search your columns for specific values.

https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM