使用 php 和 mysql 从表中获取最新值

发布于 2024-12-18 20:16:41 字数 1392 浏览 1 评论 0原文

我有下面的 sql 表结构,我尝试使用 codeigniter 从 2 个表中获取值。

table: salesman

id    sales_name     ref_id
1       kevin      174
2       mike       574
3       nick       777


table: sales_report

 id   salesman_id   product     purchased_date      dispatched 
 1    2             BF0214      04-November-2011        Yes
 2    2             CF0474      09-November-2011        No
 3    2             BF0111      10-November-2011        No
 4    3             BF0714      15-November-2011        Yes
 5    3             BF0435      15-November-2011        Yes
 6    2             BF0335      18-November-2011        Yes
 7    1             BF0714      22-November-2011        Yes
 8    1             BF0335      25-November-2011        Yes

我将 salesman_id 传递给模型以获取并在我的视图中显示值。

我的 html 表如下所示,

Ref ID   |  Salesman Name  | Last product Sold | Sold Date  | Dispatched Status

问题是我如何查询以从 salesman 表中获取 sales_name 和 ref_id ,并从 sales_report 表中获取最新的产品名称、已调度和购买日期?

例如:

   Ref ID   |  Salesman Name  | Last product Sold |   Sold Date         | Dispatched Status
    174            kevin            BF0335            25-November-2011       Yes
    574            mike             BF0335            18-November-2011       Yes
    777            nick             BF0435            15-November-2011       Yes

Im having the below sql table structure and im trying to grab values from 2 tables using codeigniter.

table: salesman

id    sales_name     ref_id
1       kevin      174
2       mike       574
3       nick       777


table: sales_report

 id   salesman_id   product     purchased_date      dispatched 
 1    2             BF0214      04-November-2011        Yes
 2    2             CF0474      09-November-2011        No
 3    2             BF0111      10-November-2011        No
 4    3             BF0714      15-November-2011        Yes
 5    3             BF0435      15-November-2011        Yes
 6    2             BF0335      18-November-2011        Yes
 7    1             BF0714      22-November-2011        Yes
 8    1             BF0335      25-November-2011        Yes

im passing the salesman_id to the model to grab and display the values in my view.

my html table is as below

Ref ID   |  Salesman Name  | Last product Sold | Sold Date  | Dispatched Status

the problem that im having how can query to get the sales_name and ref_id from the salesman table and get the most recent product name, dispatched and purchase_date from from the sales_report table?

eg:

   Ref ID   |  Salesman Name  | Last product Sold |   Sold Date         | Dispatched Status
    174            kevin            BF0335            25-November-2011       Yes
    574            mike             BF0335            18-November-2011       Yes
    777            nick             BF0435            15-November-2011       Yes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

究竟谁懂我的在乎 2024-12-25 20:16:41

使用

分组依据

排序依据

完成此操作

Use

GROUP BY

ORDER BY

for getting this done

飘逸的'云 2024-12-25 20:16:41

您需要使用 sales_report< 中的 SELECT 值/code>, 左连接 销售员 进入sales_nameref_idORDER BY purchased_date(DESC 首先获取最新的,而不是最旧的),以及 GROUP BYsalesman.id 来获取它只返回每个推销员一行。

SELECT * FROM sales_report LEFT JOIN salesman ON sales_report.salesman_id = salesman.id ORDER BY purchased_date DESC GROUP BY salesman.id

You need to use SELECT values from the sales_report, LEFT JOIN salesman to get in the sales_name and ref_id, ORDER BY the purchased_date (DESC to get the most recent one first instead of the oldest first), and GROUP BY the salesman.id to get it to only return one row per salesman.

SELECT * FROM sales_report LEFT JOIN salesman ON sales_report.salesman_id = salesman.id ORDER BY purchased_date DESC GROUP BY salesman.id
话少心凉 2024-12-25 20:16:41

您应该通过两个标准找到最后一个产品:其销售日期和 ID;因为有些产品可能一天就卖掉。那么,试试这个——

SELECT s.ref_id, s.sales_name, sr.product, sr.purchased_date, sr.dispatched FROM salesman s
  JOIN (
    SELECT salesman_id, product, purchased_date, dispatched FROM (
      SELECT salesman_id, product, purchased_date, dispatched, IF(@salesman_id = salesman_id, @i := @i + 1, @i := 1) n, @salesman_id := salesman_id
        FROM sales_report, (SELECT @i:= 0, @salesman_id = NULL) vars
      ORDER BY salesman_id ASC, purchased_date DESC, id DESC) t
    WHERE t.n = 1
      ) sr
  ON s.id = sr.salesman_id;

You should find last product by two criteras: its sold date and by its id; because some products may be sold in one day. So, try this one -

SELECT s.ref_id, s.sales_name, sr.product, sr.purchased_date, sr.dispatched FROM salesman s
  JOIN (
    SELECT salesman_id, product, purchased_date, dispatched FROM (
      SELECT salesman_id, product, purchased_date, dispatched, IF(@salesman_id = salesman_id, @i := @i + 1, @i := 1) n, @salesman_id := salesman_id
        FROM sales_report, (SELECT @i:= 0, @salesman_id = NULL) vars
      ORDER BY salesman_id ASC, purchased_date DESC, id DESC) t
    WHERE t.n = 1
      ) sr
  ON s.id = sr.salesman_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文