mysql查询分组逻辑

时间:2013-04-15 06:08:22

标签: mysql select group-by

我的这个查询从shipwynum 151513的转售表中获得6条记录: -

 select re.recdat,
        re.resaleid,
        IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' &&
         re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort,
          bosal.bogrpnam), cou1.nation)) as seller,
        IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' &&
         re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort,
          bopur.bogrpnam), cou.nation)) as buyer,
        re.benownpur,
        re.grptyppur,
        re.bogrpidpur,
        re.statuscod,
        re.showinob,
        re.benownsal,
        re.grptypsal,
        re.bogrpidsal
 from resale as re
      left join owner as owpur on owpur.ownwynum = re.benownpur
      left join owner as owsal on owsal.ownwynum = re.benownsal
      left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur
      left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal
      left join country as cou on cou.coucod = re.buynation
      left join country as cou1 on cou1.coucod = re.selnation
 where re.shipwynum = '151513' and
       re.deleted = 'N' 
 order by re.saltyp desc,
          re.recdat

这里我添加了从shipwynum 15153的上述查询中获得的样本数据屏幕截图: -

enter image description here 当我添加限制条款LIMIT 1时,它获得我预期的第一个记录。但它只适用于一艘有shipwynum = 151513的船。我想为每艘船获得第一条记录。

我为每艘船获取此类记录做了哪些更改。

非常感谢任何帮助。

感谢。

2 个答案:

答案 0 :(得分:0)

您可以将查询用作内部查询并将其分组到外部。请尝试以下查询:

select abc.recdat, abc.resaleid, abc.seller, abc.buyer,abc.benownpur,
abc.grptyppur, abc.bogrpidpur, abc.statuscod, abc.showinob, abc.benownsal, abc.grptypsal, abc.bogrpidsal

  from ( select re.recdat,
        re.resaleid,
        IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' &&
         re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort,
          bosal.bogrpnam), cou1.nation)) as seller,
        IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' &&
         re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort,
          bopur.bogrpnam), cou.nation)) as buyer,
        re.benownpur,
        re.grptyppur,
        re.bogrpidpur,
        re.statuscod,
        re.showinob,
        re.benownsal,
        re.grptypsal,
        re.bogrpidsal,
        re.shipwynum
 from resale as re
      left join owner as owpur on owpur.ownwynum = re.benownpur
      left join owner as owsal on owsal.ownwynum = re.benownsal
      left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur
      left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal
      left join country as cou on cou.coucod = re.buynation
      left join country as cou1 on cou1.coucod = re.selnation
 where re.shipwynum = '151513' and
       re.deleted = 'N' 
 order by re.saltyp desc,
          re.recdat)abc group by abc.shipwynum 

答案 1 :(得分:0)

您可以使用此查询 - 目的是为每个shipwynum获取[n] resaleids,其中[n = 2]是

SELECT (SUBSTRING_INDEX(GROUP_CONCAT(CONVERT(resaleid, CHAR(8)) ORDER BY resaleid DESC), ',', 2)) AS ID FROM resale WHERE deleted = 'N' GROUP BY shipwynum

请注意,GROUP_CONCAT结果被截断为max len 1024(group_concat_max_len)。 这可能存在性能问题,因此请随时根据需要进行更新和优化。

其他建议是设定等级,但不是如何为你工作。您可以查看排名:http://www.oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#rank

希望这有帮助。

SELECT  re.recdat,
        re.resaleid,
        IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' && re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort, bosal.bogrpnam), cou1.nation)) as seller,
        IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' && re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort, bopur.bogrpnam), cou.nation)) as buyer,
        re.benownpur,
        re.grptyppur,
        re.bogrpidpur,
        re.statuscod,
        re.showinob,
        re.benownsal,
        re.grptypsal,
        re.bogrpidsal

FROM resale AS re
      left join owner as owpur on owpur.ownwynum = re.benownpur
      left join owner as owsal on owsal.ownwynum = re.benownsal
      left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur
      left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal
      left join country as cou on cou.coucod = re.buynation
      left join country as cou1 on cou1.coucod = re.selnation 

WHERE LOCATE (re.resaleid, 
(
    SELECT GROUP_CONCAT(it.resaleid) AS ids
        FROM (
                SELECT (SUBSTRING_INDEX(GROUP_CONCAT(CONVERT(resaleid, CHAR(8)) ORDER BY resaleid DESC), ',', 2)) AS ID FROM resale WHERE deleted = 'N' GROUP BY shipwynum
            ) it
)) <> 0 
    /* AND re.deleted = 'N' */
 order by re.saltyp desc,
          re.recdat