之后的SQL顺序超过(SQL order by after more than)
我正在尝试SQL来排序结果
id | name (table name: student) ----- 1 | jhon 2 | amin 3 | heli 4 | mir 5 | mrs 6 | amr 7 | jonnathan 8 | adhy
当我使用此查询
select id from studenth where id>='3' order by id DESC limit 2
出现的结果是
id | name (table name: student) ----- 8 | adhy 7 | jonnathan
虽然我想在id = 3之后对结果进行排序,但我希望数据如下所示
id | name (table name: student) ----- 4 | mir 3 | heli
I am trying SQL to sort results
id | name (table name: student) ----- 1 | jhon 2 | amin 3 | heli 4 | mir 5 | mrs 6 | amr 7 | jonnathan 8 | adhy
When i use this query
select id from studenth where id>='3' order by id DESC limit 2
The result that appears is
id | name (table name: student) ----- 8 | adhy 7 | jonnathan
Whereas I want to sort results after id = 3, I want the data as below
id | name (table name: student) ----- 4 | mir 3 | heli
原文:https://stackoverflow.com/questions/43861135
最满意答案
目前
google-truth
没有任何内置命题来验证Collection
包含来自候选Collection
中的至少N个元素。 您可以通过查看IterableSubject
中的可用方法来确定Collection
的命题的内置词汇表。由于没有内置命题,因此在
google-truth
执行断言有两个主要选项:
- 计算您自己的匹配项的数量并用
isAtLeast
- 通过为
SubjectFactory
提供自定义的SubjectFactory
来创建您自己的匹配-N命题创建你自己的命题是更多的工作,但是让你的测试更具可读性,并在测试失败时提供更多更好的消息。 以下示例演示了这两个选项:
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.truth.FailureStrategy; import com.google.common.truth.IterableSubject; import com.google.common.truth.SubjectFactory; import org.junit.Test; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.StreamSupport; import static com.google.common.collect.Sets.newHashSet; import static com.google.common.truth.Truth.assertAbout; import static com.google.common.truth.Truth.assertThat; public class ContainsAtLeastTest { List<Integer> haystack = ImmutableList.of(1, 2, 3, 1, 2, 3); @Test public void COUNT_YOURSELF_haystack_contains_at_least_two_needles() { // No built-in way to match-N elements, so you can match yourself then compare. // This is easier to implement initially, but doesn't look as nice and gives not-so-great failure messages. Set<Integer> needles = ImmutableSet.of(0, 1, 2); long theNumberOfContainedElements = haystack.stream().distinct().filter(needles::contains).count(); assertThat(theNumberOfContainedElements).isAtLeast(2L); } @Test public void CUSTOM_PROPOSITION_haystack_contains_at_least_two_needles() { // If this is something that you test often, or if you just want the test to be more readable, // it could be worth adding your own match-N proposition. // You can word your own propositions however you like to maximize readability. // The assertAbout method is google-truth's hook into extending the built-in testing vocabulary. // You can define your own propositions by providing your own Subject class // (which is just a set of possible propositions about something) and a SubjectFactory. assertAbout(An.<Integer>iterable()).that(haystack).hasAtLeast(2).elementsFrom(1, 3, 5); } // Wrapping with "An" allows specifying the Iterable's generic type in a readable way. static class An { // assertAbout will use our factory that returns our own Subject static <T> SubjectFactory<ExtendedIterableSubject<T>, Iterable<T>> iterable() { return new SubjectFactory<ExtendedIterableSubject<T>, Iterable<T>>() { @Override public ExtendedIterableSubject<T> getSubject(FailureStrategy fs, Iterable<T> target) { return new ExtendedIterableSubject<>(fs, target); } }; } // We extend from IterableSubject so we don't lose the built-in vocabulary static class ExtendedIterableSubject<T> extends IterableSubject<ExtendedIterableSubject<T>, T, Iterable<T>> { ExtendedIterableSubject(FailureStrategy failureStrategy, Iterable<T> list) { super(failureStrategy, list); } // Alternatively, we could directly define a proposition hasAtLeastTwoOf(T... elements), // but it's nicer if min is a parameter. IterableWithMin hasAtLeast(int min) { return new IterableWithMin(min); } // Don't make this inner class extend Subject, because when starting with hasAtLeast(N), // the language of the test will only make sense if it's followed by a method defined here. // This keeps the API fluent by limiting the vocabulary. class IterableWithMin { int min; IterableWithMin(int min) { this.min = min; } @SafeVarargs final void elementsFrom(T... instances) { // The actual match-N translated into a fluent hasAtLeast(N).elementsFrom(e1, e2, ..., eM) form. Set<T> instanceSet = newHashSet(instances); long numContainedElements = StreamSupport.stream(getSubject().spliterator(), false).distinct().filter(instanceSet::contains).count(); if (numContainedElements < min) { fail(String.format("contains at least %d elements from", min), Arrays.asList(instances)); } } } } } }
There is currently no built-in proposition in
google-truth
to verify that aCollection
contains at least N elements from a set of candidates. You can determine the built-in vocabulary of propositions for aCollection
by looking at the available methods inIterableSubject
.Since there is no built-in proposition, you have two main options for performing your assertion within
google-truth
:
- Count the number of matching items yourself and assert with
isAtLeast
- Create your own match-N proposition by providing a custom
SubjectFactory
toassertAbout
Creating your own proposition is more work, but makes your test more readable and gives a lot better messages upon test failure. The following example demonstrates both options:
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.truth.FailureStrategy; import com.google.common.truth.IterableSubject; import com.google.common.truth.SubjectFactory; import org.junit.Test; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.StreamSupport; import static com.google.common.collect.Sets.newHashSet; import static com.google.common.truth.Truth.assertAbout; import static com.google.common.truth.Truth.assertThat; public class ContainsAtLeastTest { List<Integer> haystack = ImmutableList.of(1, 2, 3, 1, 2, 3); @Test public void COUNT_YOURSELF_haystack_contains_at_least_two_needles() { // No built-in way to match-N elements, so you can match yourself then compare. // This is easier to implement initially, but doesn't look as nice and gives not-so-great failure messages. Set<Integer> needles = ImmutableSet.of(0, 1, 2); long theNumberOfContainedElements = haystack.stream().distinct().filter(needles::contains).count(); assertThat(theNumberOfContainedElements).isAtLeast(2L); } @Test public void CUSTOM_PROPOSITION_haystack_contains_at_least_two_needles() { // If this is something that you test often, or if you just want the test to be more readable, // it could be worth adding your own match-N proposition. // You can word your own propositions however you like to maximize readability. // The assertAbout method is google-truth's hook into extending the built-in testing vocabulary. // You can define your own propositions by providing your own Subject class // (which is just a set of possible propositions about something) and a SubjectFactory. assertAbout(An.<Integer>iterable()).that(haystack).hasAtLeast(2).elementsFrom(1, 3, 5); } // Wrapping with "An" allows specifying the Iterable's generic type in a readable way. static class An { // assertAbout will use our factory that returns our own Subject static <T> SubjectFactory<ExtendedIterableSubject<T>, Iterable<T>> iterable() { return new SubjectFactory<ExtendedIterableSubject<T>, Iterable<T>>() { @Override public ExtendedIterableSubject<T> getSubject(FailureStrategy fs, Iterable<T> target) { return new ExtendedIterableSubject<>(fs, target); } }; } // We extend from IterableSubject so we don't lose the built-in vocabulary static class ExtendedIterableSubject<T> extends IterableSubject<ExtendedIterableSubject<T>, T, Iterable<T>> { ExtendedIterableSubject(FailureStrategy failureStrategy, Iterable<T> list) { super(failureStrategy, list); } // Alternatively, we could directly define a proposition hasAtLeastTwoOf(T... elements), // but it's nicer if min is a parameter. IterableWithMin hasAtLeast(int min) { return new IterableWithMin(min); } // Don't make this inner class extend Subject, because when starting with hasAtLeast(N), // the language of the test will only make sense if it's followed by a method defined here. // This keeps the API fluent by limiting the vocabulary. class IterableWithMin { int min; IterableWithMin(int min) { this.min = min; } @SafeVarargs final void elementsFrom(T... instances) { // The actual match-N translated into a fluent hasAtLeast(N).elementsFrom(e1, e2, ..., eM) form. Set<T> instanceSet = newHashSet(instances); long numContainedElements = StreamSupport.stream(getSubject().spliterator(), false).distinct().filter(instanceSet::contains).count(); if (numContainedElements < min) { fail(String.format("contains at least %d elements from", min), Arrays.asList(instances)); } } } } } }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
目前google-truth没有任何内置命题来验证Collection包含来自候选Collection中的至少N个元素。 您可以通过查看IterableSubject中的可用方法来确定Collection的命题的内置词汇表。 由于没有内置命题,因此在google-truth执行断言有两个主要选项: 计算您自己的匹配项的数量并用isAtLeast 通过为SubjectFactory提供自定义的SubjectFactory来创建您自己的匹配-N命题 创建你自己的命题是更多的工作,但是让你的测试更具可读性,并在测 ...
-
聪明的任何()像函数来检查是否至少有n个元素是真的?(clever any() like function to check if at least n elements are True?)[2022-07-24]
你可以简单地在序列上使用一个迭代器,并检查迭代器上的any对n返回总是为True: def check(it, num): it = iter(it) return all(any(it) for _ in range(num)) >>> check([1, 1, 0], 2) True >>> check([1, 1, 0], 3) False 这里的关键是迭代器会记住它的最后位置,因此每次调用都将从最后一个结束时开始。 并且将其all包裹起来,确保只要有一个是False它就会立即退 ... -
纯LINQ和最简单的解决方案如下所示。 static class LinqExtensions { public static IEnumerable
> Split (this IEnumerable list, int parts) { int i = 0; var splits = from item in list group item by i++ % parts int ... -
collection.Skip(Math.Max(0, collection.Count() - N)); 这种方法保留了项目顺序,而不依赖于任何排序,并且在多个LINQ提供商之间具有广泛的兼容性。 重要的是要注意不要使用负数调用“ Skip 。 一些提供者,例如实体框架,当出现负参数时,会产生一个ArgumentException。 对Math.Max的呼吁避免了这一点。 下面的类具有扩展方法的所有要点,它们是:静态类,静态方法和使用this关键字。 public static class MiscEx ...
-
你的问题对我来说有两种可能性,但也许有一些解释让你开始。 首先我需要向你解释你误解了$elemMatch的意图,在这种情况下它被滥用了。 $elemMatch的想法是创建一个实际应用于数组元素的“查询文档”。 目的是在数组中的文档中具有“多个条件”,以便在成员文档内离散匹配,而不是在整个外部文档数组内。 即: { "data": [ { "a": 1, "b": 3 }, { "a": 2, "b": 2 } ] } 即使该数组中没有实际的单个元素匹配,但以下查询仍 ...
-
检查一个集合是否仅包含另一个集合中的元素的最佳方法?(Best way to check if a collection only contains elements in another collection?)[2022-05-16]
# Using numpy对于大集合来说效率非常低,因为它会创建输入列表的整个副本。 我可能会这样做: if all(i in bits for i in input): print 'Valid input' 这是一种极其蟒蛇式的方式来写你想要做的事情,它的好处是它不会创建一个可能很大的整个list (或set ),它会停止(并返回False )第一个它遇到来自不是bits input的元素。 Your # Using numpy one is awfully inefficient for l ... -
用于3种常见项目的简化解决方案。 相关组将具有相同的rnk 。 select t.* from (select count (*) over (partition by s1.SERP_item,s2.SERP_item,s3.SERP_item) as cnt ,rank () over (order by s1.SERP_item,s2.SERP_item,s3.SERP_item) as rn ...
-
var matching = names.Where(name => !files.Any(file => file.ToLower().Contains(name.ToLower()))); 在这里工作演示 var matching = names.Where(name => !files.Any(file => file.ToLower().Contains(name.ToLower()))); Working demo here
-
从一个集合中取n个元素(Take n elements from a collection)[2022-03-22]
修复! ..感谢所有的评论。 这个工作版本建立在patrick dw的方法之上。 希望这可以帮助.. var images = jQuery('.image'); for(i = 0; i < images.length / 9; i++) { jQuery('', { id: 'conainter_' + i, html: images.slice(i*9, i*9+9) }).appendTo('body'); } 尽管我最初的回答只是提示;) FIX ! .. than ...