开发者

MySQL Query exceptions

In one page, it should show records that has the following selected month from the drop down menu and it is set in the ?month=March

So the query will do this

$sql = "SELECT * FROM schedule WHERE month = '" . Cle开发者_运维百科an($_GET['month']) . "' AND finished='0' ORDER BY date ASC";

But it shows records that has a value of 2 in the finished column and I don't want the query to include this.

I've tried

$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) . "' AND finished='0' OR finished = '1' OR finished = '3' ORDER BY date ASC";

But it shows records on different months when it shouldn't be.

So basically I want the record to exclude the records that has the value of 2 in the record that will not be shown in the page.


Your first query should do what you want. Rows with finished = '2' should not be returned.

Ignoring that there is an error in your second query that you probably should fix:

$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) .
       "' AND (finished='0' OR finished = '1' OR finished = '3') ORDER BY date ASC";

The difference is parentheses around the OR clauses because AND binds more strongly than OR.

You could also achieve the same result more concisely using IN:

$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) .
       "' AND finished IN ('0', '1', '3') ORDER BY date ASC";


AND finished='0' OR finished = '1' OR finished = '3'

to

AND finished != 2

or

AND (finished = 0 OR finished = 1 OR finished = 3)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜