簡體   English   中英

那有可能計算出兩行mysql之間有多少行?

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

我有一個如下的mysql表。 我想計算漿果和草莓之間的行數,在我的情況下是這兩個水果之間的2行。 我該如何實現?

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

試試這個查詢,

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

您可以編寫與此類似的查詢-

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

這是參考-帶字符串列的SQL Between子句

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM