简体   繁体   English

如何从输入类别多次的表中选择每个类别的一项

[英]how to select one item of each category from a table where a category is entered multple times

Product table

_________________________________

|id |name |category |date |

---------------------------------

|1 |p1 |1 |13586486|

---------------------------------

|2 |p2 |2 |13586445|

---------------------------------

|3 |p3 |1 |13567456|

---------------------------------

|5 |p5 |3 |13586422|

--------------------------------

|6 |p6 |3 |13586678|

--------------------------------

|7 |p7 |1 |13586495|

-------------------------------- this somehow my table looks, now my question is how to select one product of each category which is the latest in that category, is this possible in a single query? --------------------------------这样我的桌子看起来就好了,现在我的问题是如何选择每个类别的一种产品该类别中哪个是最新的,是否可以在单个查询中完成? i tried so many things but its too lengthy... help please 我尝试了很多事情,但时间太长...请帮助

Maybe something like this (if you want it per the highest date): 也许是这样的(如果您想在最高日期使用):

SELECT
    *
FROM
    Product AS t1
    JOIN 
    (
    SELECT 
        MAX(t.date) AS date,
        t.category
    FROM 
        Product AS t
    GROUP BY
        t.category
    )
    AS maxCat 
    ON maxCat.date=t1.date
    AND maxCat.category=t1.category

If you want it by the highest product id then you can do this: 如果您想要最高的产品ID,则可以执行以下操作:

SELECT
    *
FROM
    Product AS t1
    JOIN 
    (
    SELECT 
        MAX(t.id) AS id,
        t.category
    FROM 
        Product AS t
    GROUP BY
        t.category
    )
    AS maxCat 
    on maxCat.id=t1.id
select p1.* from Product p1 left join Product p2 on p1.category = p2.category and p1.id < p2.id where p2.id is null

This will give you latest record based on latest id as there is no other field by which I can find time of added record. 这将根据最新ID为您提供最新记录,因为没有其他字段可以找到添加记录的时间。

Edit changed select * from to select p1.* from , it will not return extra empty columns from table p2. 编辑已更改的select * fromselect p1.* from ,它不会从表p2中返回多余的空列。

In Sql server you may use this: 在Sql服务器中,您可以使用以下命令:

with productWithRows as(
select row_number() over (partition by category order by date desc) as rownb,*
from product
) as productWithRows,
select * from productWithRows where rownb=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从表的每个不同类别中选择三行? - How to select three rows from each different category from a table? 如何仅选择每个类别的一条记录? - How to select only one record of each category? 如何在代表另一个类别的表中显示来自 1 个类别 (tbl_category) 的项目标题? - How do I display item title from 1 category (tbl_category) in a table that represents another category? 在选择选项中显示每个类别及其项目 - display each category with their items from table in select option 如何在MySQL中通过一次选择为每个“类别”获得5条记录? - How can I get 5 records for each 'category' with one select in MySQL? 对于包含类别名称的每个类别行,从产品表中仅获取一行 - get only one row from products table for each category row with category name included 如何限制woocommerce中每个类别的购物车中的物品 - How to limit item in the cart for each category in woocommerce laravel:如何检查每个类别中的商品可用性? - laravel: how to check item availability in each category? MySQL从表y的类别中选择,其中Count(来自另一个表的类别产品的计数)大于零 - MySQL Select from category in table y where Count (of the category's product from another table) is greater than zero 从MySQL中的每个类别中选择2个产品 - Select 2 products from each category in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM