首页 \ 问答 \ 之后的SQL顺序超过(SQL order by after more than)

之后的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
更新时间:2022-05-28 06:05

最满意答案

目前google-truth没有任何内置命题来验证Collection包含来自候选Collection中的至少N个元素。 您可以通过查看IterableSubject中的可用方法来确定Collection的命题的内置词汇表。

由于没有内置命题,因此在google-truth执行断言有两个主要选项:

创建你自己的命题是更多的工作,但是让你的测试更具可读性,并在测试失败时提供更多更好的消息。 以下示例演示了这两个选项:

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 a Collection contains at least N elements from a set of candidates. You can determine the built-in vocabulary of propositions for a Collection by looking at the available methods in IterableSubject.

Since there is no built-in proposition, you have two main options for performing your assertion within google-truth:

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));
                    }
                }
            }
        }
    }
}

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)