1

我对这一切都很陌生。

order这是我原来的问题,我有两个表叫order_items

order_id--pupil_id --date_bought--total_price

1  ----1001---- 2013-03-07 23:35:49 - 1.00

order_id-- product_name

1 ------ product1

1 ------ product2

我想显示购买日期和总价。这将是某种加入声明,其中包含使用最新 order_id 购买的产品,该 order_id 将由学生 id 选择,可能在语句中按 desc 排序。

我不确定如何将它拼凑在一起。关于如何实现这一目标的任何想法或线索?


在史蒂夫钱伯斯让我做的那一刻,我得到了这个:

$query = mysql_query("SELECT *
FROM `order` o
JOIN `order_items` oi
ON o.order_id = oi.order_id
ORDER BY o.order_id DESC");

$query_assoc = mysql_fetch_array($query);

echo $query_assoc['product_name'];

echo $query_assoc['date_bought'];

echo $query_assoc['total_price'];

这是一个测试,看看它是否有效,它确实显示了与学生 ID 关联的最后一个订单 ID 行,但是如何检索所有行并将它们放入表中?

4

1 回答 1

0

You're actually asking two questions here:

  1. I want to display date bought and total price ....
  2. how do i retrieve all rows and place them into a table

To get a collective price, you can either do it from the database by joining the tables together, you've already done that but to get a sum to make the total price you need to group the items together. You'll need a query like:

SELECT o.order_id, o.pupil_id, o.date_bought, SUM(o.total_price) AS total_price FROM order o
LEFT JOIN order_items oi
  ON oi.order_id = o.order_id
GROUP BY o.order_id

If you want to put the list of results in a table you will need to loop though the result set you have and write that out within an HTML table.

于 2013-03-08T18:04:40.163 回答