Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchUse IN when one query should return rows whose id matches any of several values:
SELECT *
FROM mydb
WHERE id IN (5, 6);
Your original condition, id = 5 AND id = 6, asks the same row to have two different ID values simultaneously. A normal ID column stores one value per row, so that condition cannot be true.
Why AND returns no rows
AND requires every comparison to be true for the same row. For an ordinary row, id = 5 and id = 6 cannot both be true because one value cannot equal two distinct numbers at once.
Use IN for a list of selected IDs
IN (5, 6) means “the value of id is any member of this list.” It is the clearest choice when the number of alternatives may grow.
Recommended Free Tools
#1 Best Overall
SELECT *
FROM mydb
WHERE id IN (5, 6, 12, 27);
This returns every row whose ID is 5, 6, 12, or 27. The values shown are numeric literals; keep the list’s types consistent with the column where possible.
Equivalent syntax with OR
For two explicit alternatives, this form has the same meaning:
Rank #2
SELECT *
FROM mydb
WHERE id = 5
OR id = 6;
| Form | Best use |
|---|---|
id IN (5, 6) |
A readable membership test, especially as the list grows |
id = 5 OR id = 6 |
A short list where each comparison should be visible |
When IDs come from a web request
Do not build SQL by directly concatenating IDs supplied by a user or browser. Use a prepared statement and bind every ID as a separate value. A parameter marker represents one data value, not an entire variable-length list, so your application must create one placeholder for each selected ID.
PHP PDO example
$ids = [5, 6, 12];
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$sql = "SELECT * FROM mydb WHERE id IN ($placeholders)";
$stmt = $pdo->prepare($sql);
$stmt->execute($ids);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
With three IDs, the generated statement contains IN (?, ?, ?), and each array element is bound to its own marker. Prepared statements keep input data separate from SQL syntax and are the appropriate protection against SQL injection.
Handle an empty selection
An empty array would produce IN (), which is invalid SQL in MySQL. Decide what an empty selection means in your application—often return an empty result immediately or skip the query—before creating the statement.
Common mistakes
- Using
ANDbetween different IDs: useINorORfor alternative matches. - Quoting the whole list:
IN ('5,6')is one string value, not two IDs. Use separate list members, such asIN (5, 6), or separate placeholders. - Interpolating request text: casting or filtering input alone is not a substitute for binding values with a prepared statement.
- Fetching before executing: a result-fetch function can read rows only after the query has executed successfully. Older PHP examples using the removed
mysql_*API are historical; use PDO or MySQLi today.
Choosing the right condition
- Use
ANDwhen one row must satisfy different conditions, for exampleid = 5 AND status = 'active'. - Use
INwhen one column may match any value in a set. - Use
ORwhen you have a small number of alternative predicates, especially when those predicates involve different columns.
For the two IDs in your example, the direct replacement is SELECT * FROM mydb WHERE id IN (5, 6);.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




