我不确定是否可以通过一个查询在SQL本身中完成它,但这就是我要问的原因。
有没有办法获得表中出现次数最少的列值?
表格如下:
var counter = [0,1,2];
var count = counter[0];
var questions = localStorage.getItem('questions');///questions had been stored in local storage with another function
console.log(questions);
questions = $.parseJSON(questions);
if (questions != 0){
$('.question').html(questions[count]['question']);
var options = (questions[count]['options']);
options.forEach(function (item) {
$('.options').html(item);
});
var index = counter.indexOf(1);
questions = questions.splice(index, 1);
console.log(questions);
localStorage.removeItem('questions);
counter = counter.splice(index, 0);
现在有一个出现次数最少的“Carrot”值,SQL如何获得具有最少出现次数的值?有点混乱,也许你第一次尝试就不会理解。感谢。
答案 0 :(得分:1)
让我们重新创建您的测试用例:
sqlite> create table vegetables(id int, value text);
sqlite> insert into vegetables values('1', 'Banana');
sqlite> insert into vegetables values('2', 'Carrot');
sqlite> insert into vegetables values('3', 'Apple');
sqlite> insert into vegetables values('4', 'Apple');
sqlite> insert into vegetables values('5', 'Banana');
sqlite> insert into vegetables values('6', 'Apple');
sqlite> insert into vegetables values('7', 'Apple');
sqlite> insert into vegetables values('8', 'Banana');
sqlite> insert into vegetables values('9', 'Apple');
检查数据与您的数据相同:
sqlite> select * from vegetables;
1|Banana
2|Carrot
3|Apple
4|Apple
5|Banana
6|Apple
7|Apple
8|Banana
9|Apple
如果您想要出现次数最少的蔬菜:
sqlite> select value from vegetables group by value order by count(value) limit 1;
Carrot
如果您想要出现次数最多的蔬菜:
sqlite> select value from vegetables group by value order by count(value) desc limit 1;
Apple
答案 1 :(得分:0)
这是一种方法:
select value
from t
group by value
order by count(*)
fetch first 1 row only;
fetch first 1 row only
是ANSI标准语法,由多个数据库支持。有些拼写为limit
或select top 1
。
答案 2 :(得分:0)
您也可以尝试使用此方法,即使它更加复杂且不太美观:
integers