[英]How to get last value from mysql record
i'm php beginner please help me.. 我是php初学者,请帮帮我..
i have mysql record like bellow 我有像bellow的mysql记录
id name value_1 value_2
1 rakesh 100 50
2 david 150 10
3 richard 0 0
4 michael 0 0
I want last record value_1 to do some math But if last record value_1==0 means i want to go and get upper top value_1 (value_1=150) 我想要最后一个记录value_1做一些数学但是如果最后一个记录value_1 == 0意味着我想要去上面的顶部value_1(value_1 = 150)
i use bellow code but i get only last value 我使用波纹管代码,但我只得到最后一个值
$get=mysql_query("SELECT MAX(id) FROM table_name ");
$got = mysql_fetch_array($get);
$next_id = $got['MAX(id)'];
here get 3d richard value_1==0 but i want 2nd david value_1 150 这里得到3d richard value_1 == 0但我想要第二个大卫值_1 150
please help me thanks in advance... 请提前帮助我...
Your query is giving you the last row, because you are always selecting the biggest id. 您的查询为您提供了最后一行,因为您始终选择最大的ID。
Try with that: 试试看:
SELECT * FROM table_name WHERE value_1 > 0 ORDER BY value_1 DESC LIMIT 1
WHERE value_1
> 0 will select only rows where value_1
is bigger than 0. It assumes that such records exists though. WHERE value_1
> 0将只选择行,其中value_1
比0大它假定这些记录虽然存在。 Let me know if this is not your case. 如果不是这种情况,请告诉我。 ORDER BY value_1 DESC
tells the query to order the rows by value_1
in descending order and thus the desired row will be on top. ORDER BY value_1 DESC
告诉查询按value_1
按降序排序行,因此所需的行将位于顶部。 LIMIT 1
selects the first row only. LIMIT 1
仅选择第一行。 Sounds like you want: 听起来像你想要的:
SELECT MAX(id) FROM table_name WHERE value_1 <> 0
Will select the maximum ID where value_1
is not 0 将选择value_1
不为0的最大ID
Try this : 试试这个 :
$get=mysql_query("SELECT value_1 FROM table_name
WHERE value_1 != 0
ORDER BY id DESC
LIMIT 1");
I actually didn't understood the question, Here is another answer wich give you different result : 我实际上并没有理解这个问题,这是另一个给你不同结果的答案:
$get=mysql_query("SELECT value_1 FROM table_name
WHERE value_1 != 0
ORDER BY value_1 DESC
LIMIT 1");
Diff with both the queries is that one is ORDER BY id DESC
and other is ORDER BY value_1 DESC
两个查询的差异是一个是ORDER BY id DESC
而另一个是ORDER BY value_1 DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.