user3030814
user3030814

Reputation: 27

Mysql condition on WHERE clause

I m using mysql database using php to fetch data. I have distributor column, i m using WHERE clause like WHERE d_id = 1 . i have 1 to 4 distributors. On front end i have HTML select option in which if i select any distributor it shows it's data but i added an option value="0" as Total . Now if i select Total it should show all data, actually where clause should not work then. (show all distributors data)

    Dist:  Product    Sales 
1.  dis_a    abc         100
2.  dis_b    abc         50
3.  dis_c    cde         10
4.  dis_c    cde         10

Upvotes: 0

Views: 58

Answers (1)

Luth
Luth

Reputation: 11

Here is an example of a select that should do roughly what you want.

select d.[disc:], d.[Product], d.[Sales]
from your_table d
where d.your_table_seq =  case when @dist_id = 0 then d.your_table_seq else @dist_id end

The case block will allow you pull every row when the @dist_id parameter is equal to 0, but will only pull id's that are equal to @dist_id when @dist_id is not equal to 0.

Upvotes: 1

Related Questions