11

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

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

select colname from tablename where columnname=x;

Now I want this function to give all rows i.e. query equivalent to

select colname from tablename;

when I pass x="All".

I want to create a generic query that when I pass "All" then it should return me all the rows else filter my result.

user229044
  • 232,980
  • 40
  • 330
  • 338
Amandeep Singh
  • 3,754
  • 8
  • 51
  • 72

9 Answers9

15

Just leave the where condition out.

If you really want it that complicated use

where columnname LIKE '%'

which will only filter nulls.

stracktracer
  • 1,862
  • 5
  • 24
  • 37
7
select colname from tablename 
where columnname=(case when @x ="All" then columnname
                  else  @x end)
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58
6

Try this

select colname from tablename where 1=1

hope the above will work

Esh
  • 836
  • 5
  • 16
  • 43
2

where 1=1 worked for me, Although where clause was being used all records were selected.

You can also try

[any_column_name]=[column_name_in_LHL]

(LHL=left hand side.)

refer my answer for more details

saswat panda
  • 151
  • 1
  • 5
1

I had the same issue some time ago and this solution worked for me

select colname from tablename where columnname=x or x = 'ALL'
  • Care to elaborate? – RamenChef Nov 29 '16 at 20:09
  • The "or" in the where clause is doing the trick here. If the condition columnname = x is met then the where clause filters the results. Otherwhise if the x = 'ALL' then the where clause is bypassed. Thus removing the filter. – Ruben Picart Apr 26 '17 at 13:51
1
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 query will return all records from table. In Node.js where I had to pass conditions as parameter I used it like this.

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);
}
Suresh Chaudhari
  • 648
  • 1
  • 8
  • 21
0

If you have to allow 'ALL' to be passed through as the parameter value to your function, then you will need to put some manipulation code in your function to construct your SELECT statement accordingly. I.e. You can detect if the parameter has 'ALL' in it and then omit the WHERE clause from your SQL statement. If a value other than 'ALL' comes through, then you can include the WHERE clause along with the relevant filter value from the parameter.

An example of a piece of code to do this would be;

IF x = 'ALL'
THEN
   SELECT COLNAME FROM TABLENAME;
ELSE
   SELECT COLNAME FROM TABLENAME WHERE COLUMNNAME = X;
END IF;
ross_t
  • 511
  • 5
  • 10
0

It's unclear what language you're using for your function, but you have to somehow parse the 'All' prior to getting to 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
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
0

Give a conditional check in your code(assuming Java) to append the WHERE clause only when x != 'All'

mySqlQuery = "SELECT colname FROM tablename" + 
                (x.equals("All") ? "" : "WHERE columnname = "+x);
Subodh
  • 2,204
  • 18
  • 22