使用 php 和 mysql 从表中获取最新值
我有下面的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
完成此操作
Use
for getting this done
您需要使用
sales_report< 中的 SELECT 值/code>, 左连接
销售员
进入sales_name
和ref_id
,ORDER BYpurchased_date
(DESC 首先获取最新的,而不是最旧的),以及 GROUP BY 的salesman.id
来获取它只返回每个推销员一行。You need to use SELECT values from the
sales_report
, LEFT JOINsalesman
to get in thesales_name
andref_id
, ORDER BY thepurchased_date
(DESC to get the most recent one first instead of the oldest first), and GROUP BY thesalesman.id
to get it to only return one row per 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 -