简体   繁体   中英

Mysql condition on WHERE clause

I m using mysql database using php to fetch data. I have distributor column, im 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM