Hi,
I want the db to return records which have either "bmw" or "audi". Below is my code but the db only shows records which have bmw.

SELECT * FROM `details` WHERE BRAND = 'bmw' OR 'audi'

Thank you in advance.

Cheers,
Vishal

Dani AI

Generated

The query returned only the BMW rows because the WHERE clause used a bare string literal on one side of the OR instead of comparing the column to the second value. MySQL does implicit type conversion when evaluating expressions: a nonnumeric string is converted to 0 in numeric/boolean context, and 0 is false, so the OR does not add any matches (see MySQL type-conversion rules). This is why 's advice to compare the column for each value was correct.

A clearer, more maintainable way to test the column against several possible values is to use the IN operator. For example:

SELECT *
FROM details
WHERE BRAND IN ('bmw', 'audi');

The IN form is concise and expresses intent clearly (see MySQL documentation for IN). Additional tips: string comparisons depend on the column collation (case sensitivity may vary), so normalize case or use an explicit COLLATE when needed. Trim unexpected whitespace from stored values before comparing, and add an index on BRAND if this table is large and you run these lookups frequently.

References: Type Conversion in Expression Evaluation, IN operator (comparison operators).

Recommended Answers

All 2 Replies

RTFM.
And change your syntax to

SELECT * FROM `details` WHERE BRAND = 'bmw' OR [U]BRAND = [/U]'audi'

thanks smantscheff :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.