Select with if condition

Select with if condition

ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4

Hello Allan

I want to use if condition in my select query, this is my code now (not using the if condition).

$field = Field::inst('traffic_managing_lists_controller'.'macro_name')->options( 'traffic_manager_data_mapper', 'field_name', 'field_name');
$this->editor_instance->fields($field);
$this->editor_instance->fields( Field::inst('traffic_manager_data_mapper.field_name'));
$this->editor_instance->leftJoin( 'traffic_manager_data_mapper','traffic_manager_data_mapper.field_name', '=', 'traffic_managing_lists_controller.macro_name');

this code produce this query:

SELECT 
    `traffic_managing_lists_controller`.`id` as 'traffic_managing_lists_controller.id', 
    `traffic_managing_lists_controller`.`id` as 'traffic_managing_lists_controller.id', 
    `traffic_managing_lists_controller`.`macro_name` as 'traffic_managing_lists_controller.macro_name', 
    `traffic_manager_data_mapper`.`field_name` as 'traffic_manager_data_mapper.field_name' 
FROM 
    `traffic_managing_lists_controller` 
    LEFT JOIN `traffic_manager_data_mapper` ON `traffic_manager_data_mapper`.`field_name` = `traffic_managing_lists_controller`.`macro_name` 
ORDER BY 
    `traffic_managing_lists_controller`.`id` asc 
LIMIT 
    10

Now, i want to change the query to this :

SELECT 
    `traffic_managing_lists_controller`.`id` as 'traffic_managing_lists_controller.id', 
    `traffic_managing_lists_controller`.`id` as 'traffic_managing_lists_controller.id', 
    `traffic_managing_lists_controller`.`macro_name` as 'traffic_managing_lists_controller.macro_name', 
    IF(
        `traffic_manager_data_mapper`.`field_name` is null, 
        `traffic_managing_lists_controller`.`macro_name`, 
        `traffic_manager_data_mapper`.`field_name`
    ) as 'traffic_manager_data_mapper.field_name' 
FROM 
    `traffic_managing_lists_controller` 
    LEFT JOIN `traffic_manager_data_mapper` ON `traffic_manager_data_mapper`.`field_name` = `traffic_managing_lists_controller`.`macro_name` 
ORDER BY 
    `traffic_managing_lists_controller`.`id` asc 
LIMIT 
    10

does the datatables support Select with if condition?
how my code should look like?

Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    I'm sorry to say that no, it doesn't support conditional fields. You'd need to pull the data out of the table and then perform any conditional combinations of data that you want in the PHP.

    Allan

This discussion has been closed.