简体   繁体   English

那有可能计算出两行mysql之间有多少行?

[英]is that possible to count how many rows between two rows mysql?

I have a mysql table like below. 我有一个如下的mysql表。 I want to count how many rows between berry and strawberry, in my case is 2 rows between these two fruits. 我想计算浆果和草莓之间的行数,在我的情况下是这两个水果之间的2行。 How can I achieve that? 我该如何实现?

 id fruit 1 apple 2 berry 3 banana 4 pineapple 5 strawberry 

Try this query, 试试这个查询,

  $sql = "SELECT COUNT(*) as total_count FROM table_name JOIN ( SELECT MAX(id) as 
  maxid,MIN(id) as minid FROM `table_name` WHERE fruit = 'berry' OR 
  fruit= 'strawberry') as temp ON table_name.id > temp.minid AND 
  table_name.id<temp.maxid";

you can write query similar to this- 您可以编写与此类似的查询-

$sql = select * from table where fruit between 'berry' and 'strawberry';
$result = mysql_query($sql);
$rows =  mysql_num_rows($result);

here is the reference- SQL Between clause with strings columns 这是参考-带字符串列的SQL Between子句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM