简体   繁体   English

mysql选择与上次输入的行具有相同值的行

[英]mysql select rows that have a same value as the last entered row

I have created a database that adds products to a basket/cart table. 我创建了一个将产品添加到购物篮/购物车桌面的数据库。

What I want to do is check what was the last item added, and count how many times it item exists in the same table. 我想要做的是检查最后添加的项目是什么,并计算该项目在同一个表中存在的次数。

Say the table is called "basket" and the column is product. 假设该表被称为“篮子”,该列是产品。

eg: 例如:
cartid, product , user , date 软骨,产品,用户,日期
1 , 50, bob , 2016-2-20 00:48:21 1,50,鲍勃,2016-2-20 00:48:21
2 , 40, bob , 2016-2-20 00:48:23 2,40,鲍勃,2016-2-20 00:48:23
3 , 50, bob , 2016-2-20 00:48:24 3,50,bob,2016-2-20 00:48:24
4 , 30, bob , 2016-2-20 00:48:25 4,30,bob,2016-2-20 00:48:25
5 , 40, bob , 2016-2-20 00:48:27 5,40,bob,2016-2-20 00:48:27

so in the above example, the last product is 40, and it occurs 2 times... 所以在上面的例子中,最后一个产品是40,它发生了2次......

I was wondering if inner join to the same table is allowed. 我想知道是否允许内部联接到同一个表。

SELECT product,
       count(product) AS total
FROM basket
WHERE `user` like 'bob' AND product =
    (SELECT product
     FROM basket b
     ORDER BY cartid DESC LIMIT 1)
GROUP BY product

DEMO DEMO

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM