php 重复n次字符串,php – Mysql限制列值重复N次

我有两张桌子

Customer (idCustomer, ecc.. ecc..)

Comment (idCustomer, idComment, ecc.. ecc..)

显然,这两个表是连接在一起的

SELECT * FROM Comment AS co

JOIN Customer AS cu ON cu.idCustomer = co.idCustomer

有了这个,我选择与该表相关联的所有评论是客户,但现在我想限制评论数量每个客户最多2个评论.

我看到的第一件事是使用GROUP BY cu.idCustomer,但每个客户只限制1条评论,但我想为每位客户提供2条评论.

我怎样才能做到这一点?

解决方法:

MySQL中的一个选项是服务器端变量.例如:

set @num := 0, @customer := -1;

select *

from (

select idCustomer

, commentText

, @num := if(@customer = idCustomer, @num + 1, 1)

as row_number

, @customer := idCustomer

from Comments

order by

idCustomer, PostDate desc

) as co

join Customer cu

on co.idCustomer = cu.idCustomer

where co.row_number <= 2

标签:php,mysql,group-by

来源: https://codeday.me/bug/20190730/1582236.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值