English 中文(简体)
如何限制想要显示的ql数据的数量?
原标题:how to limit the amount of sql data that i want to display?

我数据库中的内容的例子:

sub-category | item
-----------------------
Fruit        | Orange
Fruit        | Apple
Fruit        | Pineapple
Fruit        | Cherry
Vegetable    | Potato
Vegetable    | Celery
Vegetable    | Ginger
Vegetable    | Carrot
Drinks       | Coffee
Drinks       | Tea
Drinks       | Coke
Drinks       | Pepsi

我试图使用以下代码,但只显示每个类别1个项目,而不是显示所有项目:

SELECT SubCategory, Item
FROM ItemList 
GROUP BY SubCategory 
ORDER BY item DESC 

我得到的是:

sub-category | item
-----------------------
Fruit        | Apple
Vegetable    | Carrot
Drinks       | Coke

我想的是:

sub-category | item
-----------------------
Fruit        | Apple
Fruit        | Cherry
Vegetable    | Carrot
Vegetable    | Celery
Drinks       | Coke
Drinks       | Coffee
问题回答

大部分执行工作都有关键词: LiMIT <#> 您可在问询结束时利用,用你所展示的浏览数取代第2号。 我不知道你如何整理你的数据,但如果你在问询结束时放弃,那就足够了。

我们不能按条款使用专家组,因为这样它只能显示每个集团的一个项目。 然而, 您也可以使用按条款分列的文件,这可用于不止一个项目。 例如......

SELECT * FROM table ORDER BY category DESC,subCategory DESC LIMIT 5;

如果你只想从每个类别中显示5级最高,那么你就能够以单一方式做到这一点。

∗∗∗ 有些人改变了原来的问题。 问题与以前完全不同。 因此,我的回答是不有用的。 *。

我建议你增加国际发展法领域,使之成为一个首要关键,国际发展法应具备。

然后,你可以使用该守则。

select t1.*
from yourtable t1
where 5 >= (select count(t2.id)
             from yourtable t2
             where t1.category= t2.category
             and t1.id >= t2.id
           )

我希望这件事能够做到。 我在我的SQL上测试了它。

在多指标类集调查中,你可以分立一栏,然后使用该栏过滤器。

SELECT
    SubCategory, 
    Item
FROM
    (
    SELECT 
        SubCategory, 
        Item, 
        ROW_NUMBER() OVER(partition SubCategory, ORDER BY SubCategory) AS seq
    FROM 
        ItemList 
    ) AS t
WHERE seq < 3





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签