2012-10-22 31 views
-1

我想:如何顯示結果並處理錯誤#1242?

if the value qty_out! = 0, then set qty_out = 0 

幫助我來顯示結果

SELECT 
    SUBSTR(stok_control.tgl_faktur,9,2) AS 'tanggal', 
    SUBSTR(stok_control.tgl_faktur,6,2) AS 'bulan', 
    CONCAT(
     (SELECT(
      IFNULL(stok_control.faktur_beli,'')) 
     )  
     , 
     (SELECT(
      IFNULL(stok_control.faktur_jual,'')) 
     ) 
    ) AS 'faktur', 
    bahan.id AS 'kode_bahan', 
    bahan.nm_bahan AS 'nama_bahan', 

    stok_control.qty_in AS 'masuk', 
    (
    SELECT IF(stok_control.qty_out != '',0,0) 
    FROM 
     stok_control 
    WHERE 
     stok_control.faktur_beli !='' 
    ) AS 'keluar' 

FROM 
    stok_control 
LEFT JOIN bahan ON stok_control.id_bahan=bahan.id 

@eggyal,我已經改變了你的代碼是:

SELECT 
    SUBSTR(sc.tgl_faktur, 9, 2) AS 'tanggal', 
    SUBSTR(sc.tgl_faktur, 6, 2) AS 'bulan', 
    CONCAT(
     IFNULL(sc.faktur_beli, ''), 
     IFNULL(sc.faktur_jual, '') 
     ) AS 'faktur', 
    b.id  AS 'kode_bahan', 
    b.nm_bahan AS 'nama_bahan', 
    (SELECT IFNULL(sc.qty_in,0)) AS 'masuk', 
    (SELECT 
     (
      IF(faktur_beli <> '', 
       0, 
       (SELECT sc.qty_out) 
      ) 
     ) 

    ) AS 'qty_out' 
FROM 
    stok_control sc LEFT JOIN bahan b ON sc.id_bahan = b.id 

我試着引用現有的但仍然錯誤...

+0

對於子查詢必須返回至多一行但返回多行的語句會發生此錯誤。在這裏檢查http://dev.mysql.com/doc/refman/5.0/en/subquery-errors.html –

回答

0

我想:

if the value qty_out! = 0, then set qty_out = 0 

你的意思是你想qty_out爲0,不論其原有的價值?

SELECT SUBSTR(sc.tgl_faktur, 9, 2) AS tanggal, 
     SUBSTR(sc.tgl_faktur, 6, 2) AS bulan, 
     CONCAT(
     IFNULL(sc.faktur_beli, ''), 
     IFNULL(sc.faktur_jual, '') 
     )       AS faktur, 
     b.id      AS kode_bahan, 
     b.nm_bahan     AS nama_bahan, 
     sc.qty_in     AS masuk, 
     0       AS qty_out 
FROM stok_control sc LEFT JOIN bahan b ON sc.id_bahan = b.id 
+0

是的,例如,初始值大於0,那麼它應該改爲0,如果初始值0,然後讓它保持 –

+0

@ user1764869:那麼就像上面那樣選擇常量'0'。 – eggyal

+0

我改變您的代碼:SELECT \t SUBSTR(sc.tgl_faktur,9,2)AS 'tanggal', \t SUBSTR(sc.tgl_faktur,6,2)AS '布蘭', \t CONCAT( \t \t IFNULL( sc.faktur_beli, ''), \t \t IFNULL(sc.faktur_jual, '') \t \t)AS 'faktur', \t \t b.id \t AS 'kode_bahan', \t b.nm_bahan AS 'nama_bahan' , \t(SELECT IFNULL(sc.qty_in,0))AS'masuk', \t(SELECT \t \t( \t \t \t IF(faktur_beli <> '', \t \t \t \t 0, \t \t \t \t(SELECT sc.qty_out) \t \t \t) \t \t) \t )AS'qty_out' FROM \t stok_control sc LEFT JOIN bahan b on sc.id_bahan = b.id –