[英]MySql: select rows between two specific values:
I have this table: 我有这张桌子:
and I'd like to select the rows between 'inizio_votazione1' e fine_votazione1'. 我想选择“ inizio_votazione1”和“ fine_votazione1”之间的行。
I tried this code: 我尝试了这段代码:
$sql = "SELECT codice FROM scansioni WHERE codice BETWEEN 'inizio_votazione1'
AND 'fine_votazione1'";
while ($row = $result->fetch_row()) {
// print_r($row);
$res = $row[0];
echo $res;
echo "<br />";
}
but I see no result. 但我看不到结果。
Which is the correct syntax to retrieve the desired result? 哪种是检索所需结果的正确语法? Thanks
谢谢
I don't think you really want to use BETWEEN
. 我不认为您真的要使用
BETWEEN
。 That will basically look for alphabetically ordered values that are between those two. 基本上,这将寻找这两个字母之间的按字母顺序排序的值。 I would think you could do something like this:
我认为您可以执行以下操作:
SELECT codice
FROM scansioni
WHERE id > (SELECT MIN(id)
FROM scansioni
WHERE codice IN ('inizio_votazione1', 'fine_votazione1'))
AND id < (SELECT MAX(id)
FROM scansioni
WHERE codice IN ('inizio_votazione1', 'fine_votazione1'))
This may not be the most elegant solution, but from what I tried, it worked. 这可能不是最优雅的解决方案,但是从我的尝试来看,它确实有效。
A better option might be to add a separate table that stores the start and end id
for each group. 更好的选择可能是添加一个单独的表,该表存储每个组的开始和结束
id
。 Then you could just get the start and end id
s for the group you want and then select all the values from scansioni that have id
s in the correct range. 然后,您可以获取所需组的开始和结束
id
,然后从scansioni中选择所有id
在正确范围内的值。
尝试这个:
SELECT codice FROM scansioni WHERE codice between 'inizio_votazione1' abd 'fine_votazione1'
Try changing: 尝试更改:
$sql = SELECT codice FROM scansioni WHERE codice BETWEEN 'inizio_votazione1'
AND 'fine_votazione1';
To: 至:
$sql = "SELECT codice FROM scansioni WHERE codice BETWEEN 'fine_votazione1'
AND 'inizio_votazione1'";
BETWEEN uses the sort order to determine the result. BETWEEN使用排序顺序来确定结果。
Also, strings need to be quoted in php. 另外,字符串需要在php中引用。
尝试这个:-
SELECT codice FROM `scansioni` WHERE id between (select id from scansioni where codice='inizio_votazione1') and (select id from scansioni where codice='fine_votazione1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.