title: MySQL Error Code: 1048: Column cannot be null
body: <p>I have a query:</p>
  
  <pre><code>SELECT 
    COUNT(*)
  FROM
    (SELECT 
      vendors_id,
      merchants_id,
      SUM(amount) AS amount,
      SUM(commission_amount) AS commission_amount 
    FROM
      (SELECT 
        vendors_id,
        merchants_id,
        amount,
        commission_amount 
      FROM
        (SELECT 
          vendors.id AS vendors_id,
          merchants_id,
          SUM(transactions_cash.amount) AS amount,
          SUM(
            transactions_cash.commission_amount
          ) AS commission_amount 
        FROM
          ibaserver.transactions_cash,
          ibaserver.vendors,
          ibaserver.merchants 
        WHERE transactions_cash.vendors_id = vendors.id 
          AND TIME &gt; 1466680920208 
          AND TIME &lt;= 1466681880067 
          AND merchants_id = merchants.id 
        GROUP BY transactions_cash.merchants_id 
        ORDER BY transactions_cash.merchants_id) a 
      UNION
      ALL 
      SELECT 
        vendors_id,
        merchants_id,
        amount,
        commission_amount 
      FROM
        (SELECT 
          vendors.id AS vendors_id,
          merchants_id,
          SUM(
            transactions_cash_archive.amount
          ) AS amount,
          SUM(
            transactions_cash_archive.commission_amount
          ) AS commission_amount 
        FROM
          ibaserver.transactions_cash_archive,
          ibaserver.vendors,
          ibaserver.merchants 
        WHERE transactions_cash_archive.vendors_id = vendors.id 
          AND TIME &gt; 1466680920208 
          AND TIME &lt;= 1466681880067 
          AND merchants_id = merchants.id 
        GROUP BY transactions_cash_archive.merchants_id 
        ORDER BY transactions_cash_archive.merchants_id) b) s) q 
  </code></pre>
  
  <p>And when the inner query</p>
  
  <pre><code>  SELECT 
      vendors_id,
      name,
      amount
    FROM .... 
  </code></pre>
  
  <p>returns the data, the result is returned successfully. If the inner query returns only NULL:</p>
  
  <p><a href="https://i.stack.imgur.com/Z4cle.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z4cle.jpg" alt="inner query result"></a></p>
  
  <p>the entire query returns an error:</p>
  
  <pre><code>Error Code: 1048
  Column 'vendors_id' cannot be null
  </code></pre>
  
  <p>How to return 0 in the case when the inner query returns all of a NULL?</p>