gpt4 book ai didi

php - 另一个表中的用户选项: what is the best practice to check if an option is there?

转载 作者:行者123 更新时间:2023-11-29 09:23:09 25 4
gpt4 key购买 nike

我有几个用户可以选择的选项,主要是为了验证他在网站上的存在。

表格是这样的:

用户:

id=1
username=stackoverflow
password=oSKAJMMS;
address=xyz
...

选项:

user_id=1
option=AD3
<小时/>
user_id=1
option=AC1

在某些时候,我需要检查他在“选项”表中是否有特定选项(例如:AD3、AC1 等),并注意“选项”表中根本不存在没有选项的用户。

对此的最佳实践是什么?我想过 JOIN,但每次阅读 stackoverflow 时我都会找到更简单、更有效的方法,所以我问你我该怎么办!

最佳答案

SELECT  *
FROM users u
WHERE EXISTS (
SELECT 1
FROM options o
WHERE o.user_id = u.id
AND o.option = 'AC1'
)

确保您在选项(user_id,选项)上有索引

关于php - 另一个表中的用户选项: what is the best practice to check if an option is there?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/851900/

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