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

  • SEARCH
  • Home
  • 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 7806571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:34:42+00:00 2026-06-02T02:34:42+00:00

I have this: $ids = 1,2,3,4,5; $sqlQuery = SELECT id, moderation_date FROM table_live WHERE

  • 0

I have this:

    $ids = "1,2,3,4,5";
    $sqlQuery = "SELECT id, moderation_date
                    FROM table_live
                    WHERE id IN (".$ids.")";

    $q = $this->CI->db->query($sqlQuery);

    if($q->num_rows() > 0) {
        foreach ($q->result() as $row) {
            $arr[] = $row;
        }
    }

    return $arr;

This is just working fine if all ids exist in table_live
and return

           array([id] => 1 [moderation_date] => 2012-04-11 12:55:57)....

The problem: If I send a list of ids 1-2-3-4-5 where only 1-2-5 match the IN LIST clause
I need to return all in list and for those don’t match the list a null value.

           array([id] => 3 [moderation_date] => null) 
  • 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-02T02:34:44+00:00Added an answer on June 2, 2026 at 2:34 am

    generate an outer join statement so that you get:

    SELECT ids.id, table_live.moderation_date
    FROM (select 1 id union all select 2 union all ....) ids
    LEFT JOIN table_live
    ON ids.id = table_live.id
    

    where ids is a subquery enumerating all the values, something like this:

    $ids = '1,2,3,4,5'
    $subquery = 'select '.str_replace(',', ' id union all select ', $ids).''
    $sql = "SELECT ids.id, table_live.moderation_date
    FROM ($subquery) ids
    LEFT JOIN table_live
    ON ids.id = table_live.id"
    

    be sure to select ids.id, not table_live.id. That way, the ids will always show up, and the moderation_date only if the corresponding row exists in table_live.

    Another approach would be to keep the query as you had it, store the result in an array, and then merge the arrays in php so that you retain all keys, and fill in the values only where the key matches in both arrays.

    I am not really sure what kind of db library you’re using so I don’t know how to obtain an array of the resultset, but suppose you would have stored the rows in a php array, using a string representation of the id as key, and the date as value, then this code should do the trick:

    $items = array(
        '1' => NULL
    ,   '2' => NULL
    ,   ...
    ); 
    //note: use string keys in order to merge!!
    $result = array_merge($items, $resultset);
    

    see: http://php.net/manual/en/function.array-merge.php

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

Sidebar

Related Questions

For example, I have this query select cat from Cat cat where cat.id in
I have this sql query retrieving IDs from a table with 3 columns: ID,
I have a query that looks like this: it takes a list of IDs
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
$content = '<h3>popular tags »</h3><hr>'; $result = $db->sql_query(SELECT tags FROM .DOWNLOAD_TABLE. ); while($row =
I have 2 tables, using an inner join to query them. SELECT COUNT(table2.id) FROM
Imagine I have the following SQL query: SELECT id,name FROM user WHERE id IN
I have an array of apps with ids and categories like this: [apps] =>
I basically have three tables, posts, images and postimages (this simply contains the ids
I have the folowing sql query: SELECT DISTINCT(tbl_products.product_id), tbl_products.product_title, tbl_brands.brand_name, tbl_reviews.review_date_added, NOW() AS time_now

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.