MySQL select where equal to multiple values(MySQL select where 等于多个值)
问题描述
我有两个数据库,我存储谁在关注谁,另一个存储用户发布的帖子.
I have two databases, where I store who is following who and another which stores the posts the user makes.
我想从以下数据库中选择用户关注的所有人,并回显出该用户关注的用户的用户名,并在帖子数据库中查询该用户的帖子.
I want to select all of the people a user is following from the following database and echo out the usernames of those who that user is following and query the posts database for posts of that user.
我的问题是如果一个用户关注多个用户怎么办,我从该用户所关注的人的用户 ID 中回显出来,然后我得到了44443344330
My problem is what if a user is following multiple users, I echoed out of the user id's of the people this user is following and I get 44443344330
当我用逗号分隔每个 id 时,我得到:
When I separate each id with commans, I get:
44,44,33,44,33,0,
所以让我们给它一个变量 $user_ids;
so let's give that a variable of $user_ids;
$user_ids = "44,44,33,44,33,0, ";
查询:
$get_posts = mysql_query("SELECT * FROM posts WHERE userid = '$user_ids'");
但它所做的只是显示第一个用户 ID 44 的记录.
but all it does is show the records of the first user id, 44.
如何检索所有用户的所有记录?
How can I retrieve all of the records for all the users?
推荐答案
查询应该是:
SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)
但是,您可能需要重新考虑您的数据模型并确保它是规范化的,以便您可以直接在数据库中表达这种结构,而无需回显到逗号分隔的字符串中.
However, you may have to rethink your data model and make sure it is normalized, so that you can express this construction directly in the databse without echoing into a comma-separated string.
为什么你有两个数据库?你的意思是两个表?
Why do you have two databases? Do you mean two tables?
这篇关于MySQL select where 等于多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL select where 等于多个值
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
