gpt4 book ai didi

mysql - 如何在检查另一个表的状态时从一个表中选择某些内容?

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

我在数据库中有两个表:

comments table                  users table
| u_id | text | | u_id | premium |
| 9 | .. | | 9 | 1 |
| 10 | .. | | 10 | 0 |
| 9 | .. |

我想从评论表中为用户选择文本,以防万一该用户具有高级状态 (1)。但是如果我想检查这个用户的状态我需要做另一个 mysql 查询。是否可以在一个查询中进行检查?

$premium=sql_select("SELECT premium FROM users WHERE u_id LIKE '".$_GET['u_id']."'");

if ($premium[1][0]=1) {
$text=mysql_query("SELECT text FROM comments WHERE u_id LIKE '".$_GET['u_id']."'");
}
else //this user has no premium account so text will not be selected.

最佳答案

以下查询连接表并获取所有 premium 1 的用户。

SELECT c.u_id, c.text FROM comments_table as c 
LEFT JOIN ON users_table as u ON u.u_id = c.u_id
WHERE u.premium = 1

关于mysql - 如何在检查另一个表的状态时从一个表中选择某些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8413813/

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