I want to know is this practically possible in sql(using php as server-side), where in you have an array of values(numbers), and you try to retrieve data based on values inside that array..
我想知道这在sql中几乎是可能的(使用php作为服务器端),其中你有一个值数组(数字),你尝试根据该数组中的值检索数据。
I have a sql table:
我有一个SQL表:
posts(
id int(11) primary key,
posts varchar(140),
user_id int(11) foreign key
);
I write a query now to retrieve 'posts':
我现在写一个查询来检索'帖子':
$query="SELECT * FROM posts WHERE user_id=(**Array of select user_id's**)";
Is there any sql in-built function to check values inside an array? or should I be using PHP for this?
是否有任何sql内置函数来检查数组中的值?或者我应该使用PHP吗?
I am actually trying to implement the twitter model of showing posts of people whom we follow.
我实际上正在尝试实现我们关注的人员帖子的推特模型。
4 个解决方案
#1
11
SQL can't parse PHP arrays. Try this:
SQL无法解析PHP数组。尝试这个:
$query="SELECT * FROM posts WHERE user_id IN ({implode(',', $userIDarray)})";
#2
12
Yes, this is easily possible. You need to look at MySQL's IN function
是的,这很容易实现。你需要看看MySQL的IN功能
Your query would be something like
你的查询会是这样的
SELECT * FROM posts WHERE user_id IN (1,2,3,4,5,6)
You can build the bit in between the parentheses in PHP using implode()
您可以使用implode()在PHP中的括号之间构建位
#3
2
Take a look at this page : WHERE ... IN. You can use the IN operator to check if a certain value exists within an list of values.
看看这个页面:WHERE ... IN。您可以使用IN运算符检查值列表中是否存在某个值。
#4
1
Yea you will have to you use following syntax
是的,你必须使用以下语法
SELECT 'value'
IN (array)
#1
11
SQL can't parse PHP arrays. Try this:
SQL无法解析PHP数组。尝试这个:
$query="SELECT * FROM posts WHERE user_id IN ({implode(',', $userIDarray)})";
#2
12
Yes, this is easily possible. You need to look at MySQL's IN function
是的,这很容易实现。你需要看看MySQL的IN功能
Your query would be something like
你的查询会是这样的
SELECT * FROM posts WHERE user_id IN (1,2,3,4,5,6)
You can build the bit in between the parentheses in PHP using implode()
您可以使用implode()在PHP中的括号之间构建位
#3
2
Take a look at this page : WHERE ... IN. You can use the IN operator to check if a certain value exists within an list of values.
看看这个页面:WHERE ... IN。您可以使用IN运算符检查值列表中是否存在某个值。
#4
1
Yea you will have to you use following syntax
是的,你必须使用以下语法
SELECT 'value'
IN (array)