MySQL select where 等于多个值

MySQL select where equal to multiple values(MySQL select where 等于多个值)

本文介绍了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 等于多个值

基础教程推荐