Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8912281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:09:36+00:00 2026-06-15T04:09:36+00:00

I got 2 columns in my database that hold text ( Products Code and

  • 0

I got 2 columns in my database that hold text (Products Code and Products Variant).

for example:
Products Code holds the following text: C050101, C070104
and
Products Variant holds the following text: RED, NULL

I want to select all the rows WHERE Code = Products Code AND Variant = Products Variant.
bases on the example I wrote above, I should get 2 rows:
first row WHERE Code=C050101 and Variant=RED
second row WHERE Code=C070104 and Variant IS NULL << IS NULL

I have no idea how to do it…
I would know how to do it if I only need to select only all the rows WHERE Code = Products Code >>

$arr = array('C050101','C070104');
$inclause = implode(',',array_fill(0,count($arr),'?'));
$stockstmt = $pdo->prepare(sprintf("SELECT `id`, `WebTitle`, `StockCode` FROM `stock` WHERE `StockCode` IN(%s)",$inclause));
$stockstmt->execute($arr);

I would really appreciate your help.
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:09:37+00:00Added an answer on June 15, 2026 at 4:09 am

    You can’t use the IN predicate for this because IN only knows how to compare with = and you need to compare differently to handle the NULLs.

    MySQL has a non-standard null-safe equal operator <=> which knows that NULL=NULL is true, and NULL = ‘RED’ is false.

    If you use something other than MySQL, your RDBMS might support IS [NOT] DISTINCT which is standard SQL syntax for a similar predicate.

    I tested the following and it works:

    $arr = array('C050101','RED', 'C070104', NULL);
    
    $sql = "SELECT `id`, `WebTitle`, `StockCode` FROM `stock`
      WHERE (`StockCode`, `StockVariant`) <=> (?, ?)
         OR (`StockCode`, `StockVariant`) <=> (?, ?)";
    
    $stockstmt = $pdo->prepare($sql);
    if ($stockstmt === false) { die(print_r($pdo->errorInfo(), true)); }
    
    $status = $stockstmt->execute($arr);
    if ($status === false) { die(print_r($stockstmt->errorInfo(), true)); }
    

    I’ll leave it to you how you want to build a list of predicates.


    Re your comment:

    It’s not a recommended practice to use the literal string ‘NULL’ to represent a missing or inapplicable value. That’s what NULL is for in SQL.

    If you want a function that makes it more dynamic to build the predicates, try this:

    function lookformultiplethings(array $arr)
    {
      global $pdo;
    
      $where = array();
      $values = array();
      foreach ((array) $arr as $term) {
        $where[] = '('
            . join(',', array_map(function ($col) {return "`$col`";}, array_keys($term)))
            . ') <=> ('
            . join(',', array_fill(0,count($term),'?'))
            . ')';
        $values = array_merge($values, array_values($term));
      }
      $sql = "SELECT `id`, `WebTitle`, `StockCode` FROM `stock` ";
      if ($where) {
        $sql .= "WHERE " . join(' OR ', $where);
      }
    
      $stockstmt = $pdo->prepare($sql);
      if ($stockstmt === false) { die(print_r($pdo->errorInfo(), true)); }
    
      $status = $stockstmt->execute($values);
      if ($status === false) { die(print_r($stockstmt->errorInfo(), true)); }
    
      print_r($stockstmt->fetchAll());
    }
    
    lookformultiplethings(array(
      array('StockCode' => 'C050101', 'StockVariant' => 'RED'),
      array('StockCode' => 'C070104', 'StockVariant' => 'NULL'),
    ));
    

    Be careful that the keys of the array you pass are legitimate column names. That is, don’t let user input ever set the column names unless you verify it against a whitelist of real columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've got a large database that I can't hold in memory at once.
I've got an Sqlite database where one of the columns is defined as TEXT
I've got two tables in a database with the following columns: Table1 athleteId |
I've got three columns inside of a div that is the full page width.
I've got some Columns in a DataGridView that have their Binding property set to
I've got a Rails app that'll be sitting on top of a legacy database
I've got a database table that represents a bunch of trees. The first three
I've got a set of database results where some of the columns have empty
I've got a database which stores pings from various places in the following format:
I have a client who got a zipped file that has all the database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.