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..
I have a sql table:
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?
I am actually trying to implement the twitter model of showing posts of people whom we follow.
We can pass an array with the help of where IN clause.
Array can be passed in WHERE clause of sql query and in PLSQL as well.
SELECT (array['one','two','three'])[state] FROM mytable WHERE id = 1; But as already stated, the CASE statement is the standard and portable method.
A query can contain both a WHERE clause and a HAVING clause. In that case: The WHERE clause is applied first to the individual rows in the tables or table-valued objects in the Diagram pane. Only the rows that meet the conditions in the WHERE clause are grouped.
Yes, this is easily possible. You need to look at MySQL's IN function
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()
SQL can't parse PHP arrays. Try this:
$query="SELECT * FROM posts WHERE user_id IN ({implode(',', $userIDarray)})";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With