MySQL where clause equals anything (SELECT * WHERE col = ANY_VALUE)

前端 未结 3 793
长情又很酷
长情又很酷 2020-12-17 09:31

I\'d like to create a query in MySQL that has an optional value. When the value is specified the query is filtered by that value, when the value is not all rows are returned

3条回答
  •  时光说笑
    2020-12-17 09:43

    Better way to do this is first generate sql query from the parameter you need to bother on, and then execute.

    function doQuery($params) {
        $query = 'SELECT * FROM mytable ';
        if (is_array($params) // or whatever your condition ) { 
            $query .= 'WHERE item = ' . $params[0];
        }
        $query .= ' ;';
    
        // execute generated query
        execute($query);
    }
    

提交回复
热议问题