Passing an array to a query using a WHERE clause

后端 未结 18 1003
情歌与酒
情歌与酒 2020-11-21 09:03

Given an array of ids $galleries = array(1,2,5) I want to have a SQL query that uses the values of the array in its WHERE clause like:



        
18条回答
  •  隐瞒了意图╮
    2020-11-21 09:26

    You may have table texts (T_ID (int), T_TEXT (text)) and table test (id (int), var (varchar(255)))

    In insert into test values (1, '1,2,3') ; the following will output rows from table texts where T_ID IN (1,2,3):

    SELECT * FROM `texts` WHERE (SELECT FIND_IN_SET( T_ID, ( SELECT var FROM test WHERE id =1 ) ) AS tm) >0
    

    This way you can manage a simple n2m database relation without an extra table and using only SQL without the need to use PHP or some other programming language.

提交回复
热议问题