Output from EXISTS



  • I have a request.

    SELECT 
      IF (
        EXISTS (
          SELECT 
            name_short,
            name_full,
            ...
          FROM
            (SELECT 
              ...
            FROM
              ...
            WHERE ...
            GROUP BY ...
            ORDER BY ...) a 
          UNION
          ALL 
          SELECT 
            name_short,
            name_full,
            ...
          FROM
            (SELECT 
              ...
            FROM
              ...
            WHERE ...
            GROUP BY ...
            ORDER BY ...) b
        ) = 0,
        'empty set',
        'not empty set'
      ) ;
    

    It works correctly, and if the request is inside the function EXISTS returns the empty line, delivers the message. 'empty set' Otherwise, the line. 'not empty set'♪ I want to get the fields. name_short and name_full in case the request is within the function EXISTS returns data (not empty line). How can we do that without duplicating the code?



  • I can suggest that:

    (
    SELECT 1 as 'is_exists'
        name_short,
        name_full,
    /* и так далее */
        limit 1
    )
    union all
    (
    select 0 as 'is_exists', null, null
    )
    limit 1
    

    But only if you need one footage. limit 1 Mandatory for corrective work in both places. Or that's how it goes:

    select (is_exists_table.name_short is not null) as is_exists,
        name_short,
        name_full
    from (
        select 1 -- just any one row
    ) anyrow left join (
        select name_short, name_full
            from /* остальной запрос */
    ) is_exists_table ON TRUE
    

    I'll get all the evidence on the condition or one footage. is_exists = 0 and the fields NULL♪ If name_short Maybe. NULL On your own, choose some other field. not null


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2