node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Select with multiple conditions and AND does not work with postgresql

Open Mamath93 opened this issue 3 years ago • 2 comments

var query = {
	text: 'SELECT * FROM advertisement WHERE (brand_class = ($1) AND product_class = ($2)) ',
	values: [brand_class,product_class]
};

    const client = await pool.connect();
    const res = await client.query(query);

I does not work with pg, but it works by directly querying postgresql.: SELECT * FROM advertisement WHERE (brand_class ='body_cosmetic' AND product_class ='skin_tone');

This one works: var query = { text: 'SELECT * FROM advertisement WHERE product_class =($1)', values: product_class };

But AND or other operator is not well handled by pg....

Mamath93 avatar Feb 25 '22 20:02 Mamath93

Is there an error? What do “does not work” and “not well handled” mean?

charmander avatar Feb 25 '22 23:02 charmander

Thanks @charmander for your quick answer.

I've and advertisement table that contains product_class and brand_class elements.

Let's say product_class = product_class_1 & brand_class = brand_class_1

When I built my query with: var query = { text: 'SELECT * FROM advertisement WHERE product_class =($1)', values: product_class_1 }; Corresponding advertisement is well returned

When I built my query with: var query = { text: 'SELECT * FROM advertisement WHERE brand_class =($1)', values: brand_class_1 }; Corresponding advertisement is well returned

When I built my query with: var query = { text: 'SELECT * FROM advertisement WHERE (brand_class = ($1) AND product_class = ($2)) ', values: [brand_class_1,product_class_1] }; Nothing is returned.

When I directly requests postgre: SELECT * FROM advertisement WHERE (brand_class =brand_class_1 AND product_class =product_class_1);

Corresponding advertisement is well returned.

I don't understand why WHERE with multiple conditions (AND) does not return anything.

Thx for your help !

Mamath93 avatar Feb 26 '22 09:02 Mamath93