[英]MySQL get data from the table and check if userid is present in the table row
I have a table called " session ", the table has a column called " userid ". 我有一个名为“ session ”的表,该表有一个名为“ userid ”的列。
Example of the table: 表格示例:
ROW userid
-------------
1 61
2 128
3 84
Now in my site I have articles listed all on ONE page, EACH article is written by a user, and EACH user has it's own userid 现在,在我的网站上,我的文章全部列在一个页面上,每个文章都是由用户撰写的,每个用户都有自己的用户ID
What I'm trying to achieve is: IF the user(userid=X) which wrote the article EXISTS in session table show a text.. 我要实现的目标是:如果在会话表中写文章EXISTS的用户(userid = X)显示文本。
Here is how I'm trying to get the userid from the session table -> 这是我试图从会话表中获取用户ID的方式->
SELECT userid FROM #session
But I guess with this code it only takes ONE userid from the column not ALL of them... 但是我猜想用此代码它只从列中获取一个userid而不是所有它们...
Can someone give me a hand on this please? 有人可以帮我吗? Thank you 谢谢
Hope you have also database table for articles (table article with column userid) , you may use inner join in your query: 希望您也有用于文章的数据库表(具有userid列的表文章),可以在查询中使用内部联接:
Select userid FROM article
JOIN session using(userid)
This will get all users that are in session table and have the article 这将获取会话表中所有拥有文章的用户
The following will select ALL userid's from session: 下面将从会话中选择所有用户标识:
SELECT userid FROM session
...which is almost exactly the code you mentioned. ...几乎就是您提到的代码。 However, you don't need to get a list of userid's, you just need to see if a certain id exists in the table. 但是,您不需要获取用户ID的列表,只需要查看表中是否存在某个ID。 So, what you do is you count the number of instances that the userid is a specific value, which would look something like this (in php, using mysqli): 因此,您要做的是计算userid是特定值的实例数,该值看起来像这样(在php中,使用mysqli):
function is_id_in_session($db, $userid) {
$total_result = $db->query("SELECT COUNT(userid) FROM session WHERE userid = {$userid}");
$total = $total_result->fetch_row();
return ($total[0] > 0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.