gpt4 book ai didi

mysql - 统计用户是否已达到借用限制

转载 作者:可可西里 更新时间:2023-11-01 07:09:04 25 4
gpt4 key购买 nike

我已经用表格和数据设置了一个 fiddle here

我正在尝试编写一个 sql 来检查用户是否已达到每个类别的借用限制。

现在,它是使用几个互相调用的 sql 语句完成的。

但是方法很简单。memId 和 id 来自查询字符串。

$medId = $_POST['memId']; Using 1 for this example. This is the members Id.
$id = $_POST['id']; Using 4 for this example. This is the item being lent.

之后我做:

select id, holder from collection_db where id = 4 // We have a valid item

select borrowMax from collection_db where id = (holder from the previous select) and category = 10 //Result = 2. Category indicates its a label and not a borrowable item.

select count(borrowedId) from lendings where memId = 1 and holder = (holder from the 1st query) //He's borrowed 2, under 1, so cant borrow any more. User 2 may borrow however.

if (count => borrowMax) {echo 'Cannot borrow more.';} else {echo 'Added to'}

如何将其组合成单个 sql 还是最好保留这种方式?

最佳答案

这似乎产生了一个正确的结果集:

SELECT col1.id, col1.holder, col2.borrowMax, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder
WHERE col1.id = $id
AND col2.category = 10
AND lend.memId = $medId

关于mysql - 统计用户是否已达到借用限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16716447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com