如何获取最新数量的sql子查询

时间:2016-02-25 08:22:18

标签: php mysql sql

我需要获得每个库存商的最新金额交易,而不是他们的交易总额。

有两个表库存

Stockist table

    public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    Map<String, String> states = new HashMap<String, String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox checkboxMovies = (CheckBox) findViewById(R.id.chkInterestsMovies);
        checkboxMovies.setOnCheckedChangeListener(this);
        CheckBox checkboxAnimals = (CheckBox) findViewById(R.id.chkInterestsAnimals);
        checkboxAnimals.setOnCheckedChangeListener(this);
        CheckBox checkboxShopping = (CheckBox) findViewById(R.id.chkInterestsShopping);
        checkboxShopping.setOnCheckedChangeListener(this);
        CheckBox checkboxBooks = (CheckBox) findViewById(R.id.chkInterestsBooks);
        checkboxBooks.setOnCheckedChangeListener(this);
        CheckBox checkboxRestaurants = (CheckBox) findViewById(R.id.chkInterestsRestaurants);
        checkboxRestaurants.setOnCheckedChangeListener(this);
        CheckBox checkboxComputers = (CheckBox) findViewById(R.id.chkInterestsComputers);
        checkboxComputers.setOnCheckedChangeListener(this);
        CheckBox checkboxTV = (CheckBox) findViewById(R.id.chkInterestsTV);
        checkboxTV.setOnCheckedChangeListener(this);
        CheckBox checkboxPubs = (CheckBox) findViewById(R.id.chkInterestsPubs);
        checkboxPubs.setOnCheckedChangeListener(this);
        CheckBox checkboxDancing = (CheckBox) findViewById(R.id.chkInterestsDancing);
        checkboxDancing.setOnCheckedChangeListener(this);
        CheckBox checkboxMusic = (CheckBox) findViewById(R.id.chkInterestsMusic);
        checkboxMusic.setOnCheckedChangeListener(this);
        CheckBox checkboxCoffe = (CheckBox) findViewById(R.id.chkInterestsCoffe);
        checkboxCoffe.setOnCheckedChangeListener(this);
        CheckBox checkboxOther = (CheckBox) findViewById(R.id.chkInterestsOther);
        checkboxOther.setOnCheckedChangeListener(this);

    }


    @Override
    public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            states.put(String.valueOf(buttonView.getId()), buttonView.getText().toString());
        } else {
            states.remove(buttonView.getId());
        }
        Button btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (states.size() == 5) {
                    List<String> selectedStrings = new ArrayList<String>(states.values());
                    TextView chkEditText = (TextView) findViewById(R.id.chkEditText);
                    chkEditText.setText(selectedStrings.toString());
                } else {
                    Toast.makeText(MainActivity.this, "Only 5 are allowed", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

交易表

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

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

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

解决方案:

$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++;
}

谢谢大家

3 个答案:

答案 0 :(得分:2)

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

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')

答案 1 :(得分:0)

按金额尝试订单

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

答案 2 :(得分:-2)

试试这个:

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