MYSQL php: query for multiple rows and return value if the identifier "WHERE IN" is not found

I am developing a Wordpress website that manages events.

The problem is that I need to get the names of the event names from their respective identifiers in a single query. I am trying to get multiple rows from a single MYSQL query, which works fine with " WHERE IN ({$IDs})":

$events_implode = implode(',',$events);
$events_titles = $wpdb->get_results("SELECT `title` FROM `wp_events` WHERE `id` in ({$events_implode}) ORDER BY FIELD(id,{$events_implode}",ARRAY_A);

However, I need to return the value of / string if one of the IDs was not found in the request instead of not returning anything.

Example: If $events_implodeit matters: 318,185,180,377and the events with identifiers 180 and 185 do not exist (for example, they are deleted), I return event names only for 318 and 377:

Array
(
    [0] => Array
        (
            [title] => Title 1
        )

    [1] => Array
        (
            [title] => Title 4
        )

)

while I need to return something like:

Array
(
    [0] => Array
        (
            [title] => Title 1
        )

    [3] => Array
        (
            [title] => Title 4
        )

)

I tried IFNULL:

$events_titles = $wpdb->get_results("SELECT IFNULL( (SELECT `title` FROM `wp_events` WHERE `id` in ({$events_implode}) ORDER BY FIELD(id,{$events_implode}) ),'not found')",ARRAY_A);

, :

"mysqli_query(): (21000/1242): Subquery returns more than 1 row"

? !

+4
3

, id, :

SELECT id, title FROM ... etc

( )

array(
    0 => array("id" => 318, "title" => "title for 318"),
    1 => array("id" => 377, "title" => "title for 377")
)

, , $events, :

foreach($event_titles as $row) {
    $hash[array_search($row['id'], $events)] = $row['title'];
};

$events_titles = $hash;

( ):

array(
    0 => array("title" => "title for 318"),
    3 => array("title" => "title for 377")
)

. :

$ids = array_intersect($events, array_column($event_titles, 'id'));
$event_titles = array_combine(array_keys($ids), array_column($event_titles, 'title'));

Alternative

, , , , :

$subsql = implode(
    ' UNION ALL ', 
    array_map(function ($event) { return "SELECT $event AS id"; }, $events)
); 

$sql = "SELECT    wp_events.title 
        FROM      ($subsql) AS eid
        LEFT JOIN wp_events ON wp_events.id = eid.id
        ORDER BY  eid.id";

$events_titles = $wpdb->get_results($sql, ARRAY_A);

$subsql , (, ):

SELECT 318 AS id UNION ALL
SELECT 185 AS id UNION ALL
SELECT 180 AS id UNION ALL
SELECT 377 AS id

$events_titles , , , . , , , :

array(
    0 => array("title" => "title for 318"),
    1 => array("title" => null), // because no match with 185
    2 => array("title" => null), // because no match with 180
    3 => array("title" => "title for 377")
)

, null. null (0... 3) , :

$event_titles = array_filter($event_titles, function ($title) { return $title; });

( ):

array(
    0 => array("title" => "title for 318"),
    3 => array("title" => "title for 377")
)
+2

, , double

"SELECT  t.index, a.`title` 
 FROM `wp_events` as a 
 INNER JOIN ( select  1 as index , 318 as id
             from dual 
             union 
             select 2, 185
             from dual
             union 
             select 3, 180
             from dual
             union 
             select 4, 377
             from dual                 

             ) t  on t.id = b.id
ORDER BY t.index"
+1

, . mysql- implode, . ( , , ), , , , KISS ( , ).

: ?

0

Source: https://habr.com/ru/post/1648915/


All Articles