如何在PHP-MySQL中按降序获取特定客户的交易记录?

时间:2017-09-04 18:22:02

标签: php mysql mysqli

我想显示存储在MySQL表中的某些特定客户的交易记录。

我尝试了以下内容:

def spawn_process(x):
    g = tf.Graph()
    sess = tf.Session(graph=g)
    with g.as_default():
        meta_graph = tf.train.import_meta_graph('model.meta')
        meta_graph.restore(sess, tf.train.latest_checkpoint(chkp_dir)
        x_ph = tf.get_collection('x')[0] # placeholder tensor that we use to pass in new x's to do inference on.
        pred = tf.get_collection('pred')[0] # tensor responsible for computing prediction given x
    prediction = sess.run(pred, feed_dict={x_ph: x})

它根据我的要求显示特定客户的交易记录,但它通过“cust_id”分组显示记录。我需要通过按降序排序“id”来显示它们。

请帮忙!

1 个答案:

答案 0 :(得分:0)

您可以使用SUB QUERIES并在一个SQL语句本身中获取结果。 喜欢 -

<?php
    $stmt0 = $mysqli->prepare("SELECT t.id,t.description,t.amount,t.date FROM transactions t WHERE t.cust_id = (SELECT id FROM customers WHERE status = ?) ORDER BY t.id DESC");
    $stmt0->bind_param('i',$status);
    $stmt0->execute();
    $stmt0->store_result();
    $stmt0->bind_result($id,$description,$amount,$date);
    //PRINT/echo $id,$description,$amount,$date to the front end now
?>

这应该按$id,$description,$amount,$date的降序返回$id个值。

另一种方法是使用SQL JOINS(优先于子查询,尽管内部执行几乎相同)

$stmt0 = $mysqli->prepare("SELECT t.id,t.description,t.amount,t.date FROM transactions t INNER JOIN customers c on t.cust_id=c.id WHERE c.status = ? ORDER BY t.id DESC");
//rest of the code remains same