Getting all results using the where clause

I have a function that takes an argument, which is used in where where

function (string x) -> Now this will create a sql query that gives

select colname from tablename where columnname=x;

Now I want this function to output all the lines, i.e. equivalent query

select colname from tablename;

when I pass x = "Everything."

I want to create a general query that when I pass "Everything", then it should return to me all the other lines that filter my result.

+5
source share
8 answers

Just leave the condition.

If you really want this difficult use

where columnname LIKE '%'

which will only filter zeros.

+8
source
select colname from tablename 
where columnname=(case when @x ="All" then columnname
                  else  @x end)
+6
source

select colname from tablename where 1=1

,

+2

"" , , SELECT . , "ALL", WHERE SQL. , "ALL", WHERE .

:

IF x = 'ALL'
THEN
   SELECT COLNAME FROM TABLENAME;
ELSE
   SELECT COLNAME FROM TABLENAME WHERE COLUMNNAME = X;
END IF;
0

, , - "" sql:

public void query(String param) {
  String value = "":
  switch (param) {
    case 'All':
      value = "*";
      break;
    default:
      value = param;
  }
  String sql = "select colname from tablename where colname="+value;
  //make the query
}
0

Provide conditional verification in code (suppose Java) to add a clause WHEREonly whenx != 'All'

mySqlQuery = "SELECT colname FROM tablename" + 
                (x.equals("All") ? "" : "WHERE columnname = "+x);
0
source

I had the same problem a while ago and this solution worked for me

select colname from tablename where columnname=x or x = 'ALL'
0
source
SELECT * FROM table_name WHERE 1;
SELECT * FROM table_name WHERE 2;
SELECT * FROM table_name WHERE 1 = 1;
SELECT * FROM table_name WHERE true;

Any of the above queries will return all records from the table. In Node.js, where I had to pass conditions as a parameter, I used it as follows.

const queryoptions = req.query.id!=null?{id : req.query.id } : true;
let query = 'SELECT * FROM table_name WHERE ?';
db.query(query,queryoptions,(err,result)=>{
res.send(result);
}
0
source

All Articles