Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all results using where clause

Tags:

sql

mysql

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.

like image 379
Amandeep Singh Avatar asked Jul 27 '12 06:07

Amandeep Singh


People also ask

WHERE clause pass multiple values?

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

How do I get all the values of a column in SQL?

To retrieve all columns, use the wild card * (an asterisk). The FROM clause specifies one or more tables to be queried. Use a comma and space between table names when specifying multiple tables. The WHERE clause selects only the rows in which the specified column contains the specified value.

Can we use SUM function in WHERE clause?

In SQL, we use the SUM() function to add the numeric values in a column. It is an aggregate function in SQL. The aggregate function is used in conjunction with the WHERE clause to extract more information from the data.

Can you write SELECT query with WHERE condition?

The WHERE clause is not only used in the SELECT statement, but it is also used in the UPDATE, DELETE statement, etc., which we would examine in the subsequent chapters.


4 Answers

Just leave the where condition out.

If you really want it that complicated use

where columnname LIKE '%'

which will only filter nulls.

like image 109
stracktracer Avatar answered Sep 21 '22 12:09

stracktracer


select colname from tablename 
where columnname=(case when @x ="All" then columnname
                  else  @x end)
like image 38
Joe G Joseph Avatar answered Sep 21 '22 12:09

Joe G Joseph


Try this

select colname from tablename where 1=1

hope the above will work

like image 20
Esh Avatar answered Sep 20 '22 12:09

Esh


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

like image 39
saswat panda Avatar answered Sep 23 '22 12:09

saswat panda