如何在multi-pal列上使用SQL distinct

时间:2015-07-06 12:06:50

标签: mysql distinct

下图显示了数据库表记录,我需要的是根据trno获得不同的记录。专栏我试过这个查询,

SELECT distinct
    trno,
    srno,
    DATE_FORMAT(branch_transfer.date,'%d/%m/%Y')as transferdate,
    branch_transfer.product_code,
    product_master.product_name,
    product_master.product_desc,
    qty,
    branch_code,
    company_master.company_name,
    batch_code
    FROM
    branch_transfer INNER JOIN company_master
    ON 
    branch_transfer.branch_code = company_master.company_id
    INNER JOIN product_master
    ON
    branch_transfer.product_code=product_master.product_code
    where 
    branch_transfer.dstatus=1 and qty>0
    order by trno desc

enter image description here

但是这个查询不能像我想的那样工作,如何在multi-pal列上应用distinct。或者我应该如何实现这一目标?我需要不同的值取决于trno。

1 个答案:

答案 0 :(得分:0)

Distinct从“选择”列表中删除重复项。所以你只会看到不同的trno值:

trno,
srno,
DATE_FORMAT(branch_transfer.date,'%d/%m/%Y')as transferdate,
branch_transfer.product_code,
product_master.product_name,
product_master.product_desc,
qty,
branch_code,
company_master.company_name,
batch_code

是一样的......

我怀疑您想通过trno对数据进行分组。问题就在于你。你想如何处理trno重复的值?即你想拿最大的一个?也许总结一下?