是可以计算mysql两行之间有多少行?

时间:2016-01-19 05:01:57

标签: php mysql

我有一个像下面这样的mysql表。我想要计算浆果和草莓之间的行数,在我的情况下,这两个水果之间是两行。我怎样才能做到这一点?



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




2 个答案:

答案 0 :(得分:4)

尝试此查询,

  $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";

答案 1 :(得分:1)

您可以编写与此类似的查询 -

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

这里是参考 - SQL Between clause with strings columns