Checking value in an array inside one SQL query with WHERE clause

前端 未结 4 621
萌比男神i
萌比男神i 2021-01-04 03:22

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 ins

相关标签:
4条回答
  • 2021-01-04 03:57

    SQL can't parse PHP arrays. Try this:

    $query="SELECT * FROM posts WHERE user_id IN ({implode(',', $userIDarray)})";
    
    0 讨论(0)
  • 2021-01-04 04:00

    Yea you will have to you use following syntax

    SELECT 'value'
     IN (array)
    
    0 讨论(0)
  • 2021-01-04 04:09

    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.

    0 讨论(0)
  • 2021-01-04 04:10

    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()

    0 讨论(0)
提交回复
热议问题