mysql - 从 MySQL 连接表中获取一列

标签 mysql sql

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

现在在查询中我有一个名为 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)

最佳答案

哇,这是一个很大的问题。我在构建的查询中遇到了类似的问题,并发现 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 ...

关于mysql - 从 MySQL 连接表中获取一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18457719/

相关文章:

mysql - SQL 查询带来字段中具有最低值的链接记录

sql - OrientDB - 带时区的查询

sql - 来自 pg_views 查询的不完整信息

mysql - 如何每隔几分钟自动增加mysql表的一个字段值

sql - 将 CSV 文件导入 SQL Server

mysql - 为什么 MySQL 在使用 DATE (`table` .`column` 时会删除我的索引)

PHP。如何从 2 个 mysql 表而不是 1 个表中获取数据

mysql - 使用连接将带有子查询的 MySQL 查询转换为单个查询

php - 加载结果文本 PHP

php - mysql/php 循环有多种形式还是只有一种?