从MySQL连接表的一列

时间:2013-08-27 05:34:55

标签: mysql sql

我在MySQL中有一个查询,我正在使用它来制作一个水晶报告。

现在在查询中我有一个名为scan_mode的列,它来自gfi_transaction表。我在报告中使用的scan_mode来抑制某些部分。但有些时候,某些交易ID的值为空。

所以现在我想将此scan_mode作为单独的查询,以便它可以工作。 任何人都可以帮助我如何修改以下查询,只采取scan_mode列。

SELECT 
    cc.cost_center_code AS cccde,
    cc.name AS ccnme,gf.scan_mode,
    cc.cost_center_id AS ccid,
    site.name AS siteme,
    crncy.currency_locale AS currency_locale,
    cntry.language AS LANGUAGE,
    cntry.country_name AS cntrynm,
    crncy.decimal_digits AS rnd,
    gf.transaction_no AS Serial_No,
    brnd.name AS brand_name,
    rsn.description AS reason,
    gf.comment AS COMMENT,
    ts.status_description AS STATUS,
    DATE_FORMAT(gf.created_date,'%d/%m/%Y') AS created_date,
    gf.created_by AS created_by,
    IFNULL(gf.approval_no,'Not authorized') AS Trans_no,
    gf.approved_date AS approval_dt,
    gf.approved_by AS approved_by,gf.status AS status1,
    IFNULL(loc.cost_center_code,cc.cost_center_code) AS cur_location,
    gf.document_ref_no,gf.document_ref_type,
    ,DATE_FORMAT(document_ref_date1,'%d/%m/%Y')) AS invoice_no
        FROM 
    gfi_transaction gf
    INNER JOIN gfi_instruction gfn ON (gf.transaction_id=gfn.transaction_id)
    INNER JOIN gfi_document_instruction doc ON (gf.ref_transaction_no =      doc.document_instruction_id)
    INNER JOIN reason rsn ON (gf.reason_id = rsn.reason_id)
    INNER JOIN gfi_status ts ON (gf.status = ts.gfi_status_id)
    INNER JOIN transaction_type tt ON (gf.transaction_type_id = tt.transaction_type_id)
    INNER JOIN brand brnd ON(gf.brand_id=brnd.brand_id)
    -- cc details
    INNER JOIN cost_center cc ON (brnd.parent_brand  = cc.brand_id OR gf.brand_id = cc.brand_id)
    INNER JOIN site site ON(cc.site_id = site.site_id)
    INNER JOIN country cntry ON (site.country_id = cntry.country_id)
    INNER JOIN currency crncy ON (cntry.currency_id=crncy.currency_id)
    LEFT OUTER JOIN alshaya_location_details loc ON 
        (gf.brand_id = loc.brand_id AND loc.cost_center_id =   gf.cost_centre_id)
    LEFT OUTER JOIN alshaya_location_details locto ON 
        (locto.cost_center_id = gf.from_cost_center_id)         
WHERE 
    gf.transaction_id='{?TransID}'
    AND rsn.transaction_type_id IN (10,11,14)

2 个答案:

答案 0 :(得分:0)

哇,这是个大问题。我在构建的查询中遇到了类似的问题,并发现if语法是我问题的解决方案。在这个问题中也回答了这个问题:MYSQL SELECT WITHIN IF Statement

$psdb->query = "SELECT count, s.classid,
    if (k.sic != k.siccode, k.siccode, s.siccode) as siccode,
    if (k.sic != k.siccode, k.sicdesc, s.sicdesc) as sicdesc,
    if (k.sic != k.siccode, k.sicslug, s.sicslug) as sicslug
  FROM ...

答案 1 :(得分:0)

看起来scan_mode列来自“gfi_transaction”表,它似乎是您查询中的主表。如果此列为null,则表示此表本身具有此列的NULL值。在查询中单独考虑它不会解决您的问题。尝试使用默认值替换null并在代码中处理它。您可以使用ifnull(scan_mode,'default')

添加默认值而不是NULL