【问题标题】:How to get latest amount of sql subqueries如何获取最新数量的 sql 子查询
【发布时间】:2016-02-25 08:22:18
【问题描述】:

我需要获取每个经销商的最新交易金额,而不是他们的交易总额。

有两个表库存商

库存表

stid  memberid stockistname     area_c

1      4         Roger           Sp Park
2      6         John            Little Garden
3      77        Lily            White D
4      32        Meredith        Dare street

交易表

trans_id  memberid  trans_type    amount     trans_date

300      4             cp         250        2015-12-01 00:00:56
301      6             cp         100        2015-12-01 01:20:56
302      6             cp         130        2015-12-03 11:03:51
303      77            cp          74        2015-12-03 13:03:51
304      32            cp          25        2015-12-04 11:00:02
305      6             cp         425        2015-12-04 17:00:02
306      4             cp         235        2015-12-05 06:00:02

我的查询:

SELECT * FROM stockist  LEFT JOIN ( 
SELECT * FROM transactions WHERE trans_type='cp' ORDER BY trans_id DESC LIMIT 1 ) 
transactions ON stockist.memberid=transactions.memberid ORDER BY stockist.stockistname ASC

结果是:

 John $0
 Lily $0
 Meredith $0
 Roger $0

我得到了所有库存商名称但数量为零的列表, 它应该显示

John $425
 Lily $74
 Meredith $25
 Roger $235

实际上,在我的 PHP 代码中,我的代码看起来像这样,但速度还不够快。我希望让它更快。

$x=1;
$sql = mysql_query("SELECT * FROM stockist ORDER BY stockistname ASC");
while($row=mysql_fetch_array($sql)){
$memberid=$row['memberid'];
$stockistname=$row['stockistname'];

   $sql2 = mysql_query("SELECT * FROM transactions WHERE memberid='$memberid'  AND  trans_type='cp' ORDER BY trans_id LIMIT 1");
   $row2=mysql_fetch_array($sql2);
   $latest_amount=$row2['amount'];

   echo "<p> $x - $stockistname $ $latest_amount </p>";

$x++;
}

解决方案:

select * from
stockist, (select * from transactions where trans_type='cp' ORDER BY trans_id DESC) transactions
where
stockist.memberid=transactions.memberid 
GROUP BY memberid
ORDER BY stockist.stockistname ASC

谢谢大家

【问题讨论】:

  • 你为什么要在你的连接中做一个子查询?

标签: php mysql sql


【解决方案1】:

您的限制导致了问题。尝试使用相关查询来比较最大日期,如下所示:

SELECT * FROM stockist s
LEFT OUTER JOIN transactions t
 ON(t.memberid = s.memberid and t.trans_type = 'cp')
WHERE t.trans_date = (select max(f.trans_date) from transactions f
                      where f.memberid = t.memberid and f.trans_type = 'cp')

【讨论】:

  • 打败我!您还将子查询移出联接
  • 什么都没发生是什么意思?此查询返回的内容
【解决方案2】:

尝试按金额排序

SELECT * FROM stockist   
LEFT JOIN  transactions  ON  stockist.memberid = transactions.memberid 
WHERE trans_type='cp' 
GROUP BY stockistname ORDER BY amount DESC LIMIT 1

【讨论】:

    【解决方案3】:

    试试这个:

    select stockist.stockistname, amount from
    stockist, (select memberid,amount,MAX(trans_date)  from transactions where trans_type='cp' GROUP BY memberid) transactions
    where
    stockist.memberid=transactions.memberid
    ORDER BY stockist.stockistname ASC
    

    【讨论】:

    • 是的,它确实有效,但金额不是最新的。但是,感谢您的代码,我能够得到我正在寻找的答案。谢谢
    猜你喜欢
    • 2010-09-08
    • 2016-05-09
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多