如何显示结果和处理错误#1242?

时间:2012-10-22 09:30:16

标签: mysql mysql-error-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

我试图引用现有的但仍然是错误的......

1 个答案:

答案 0 :(得分: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