October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Select Multiple IDs in One MySQL Query

To retrieve several selected records in one MySQL query, use WHERE id IN (5, 6) or the equivalent OR conditions. For web input, bind one placeholder per ID.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 AND between different IDs: use IN or OR for alternative matches.
  • Quoting the whole list: IN ('5,6') is one string value, not two IDs. Use separate list members, such as IN (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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing the right condition

  • Use AND when one row must satisfy different conditions, for example id = 5 AND status = 'active'.
  • Use IN when one column may match any value in a set.
  • Use OR when 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);.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.