2013-10-24 192 views
0

選擇所有行(取消WHERE子句)的最佳值是什麼?MYSQL如何取消或無效WHERE子句中的WHERE子句

<select id="ddlSelect"> 
    <option value = ???? > -- SELECT ALL -- </option> 
    <option value = "1" > 1 </option> 
    <option value = "2" > 2 </option> 
    </select> 

Select查詢

"SELECT EMPLOYEE_NAME WHERE EMPLOYEE_DEPARTMENT_ID = " + ddlSelect.Value; 

對於-- SELECT ALL --我試過1 OR 1 =1。它工作正常,但沒有更好的方法?

+0

查看我的答案貼http://stackoverflow.com/questions/19563543/mysql-how-to-cancle-or-void-where-clause-within-where-clause/19563640#19563640 –

回答

2
<select id="ddlSelect"> 
    <option value = "all" > -- SELECT ALL -- </option> 
    <option value = "1" > 1 </option> 
    <option value = "2" > 2 </option> 
</select> 

而在你.NET代碼

String strQuery = "SELECT EMPLOYEE_NAME FROM table_name"; 

if(ddlSelect.Value != "all") { 
    strQuery += " WHERE EMPLOYEE_DEPARTMENT_ID = " + ddlSelect.Value; 
} 

對不起,我am't .NET的熟悉

0
var query = ''; 
if($('#ddlSelect').val() > 0) 
    query = "SELECT EMPLOYEE_NAME from table_name WHERE EMPLOYEE_DEPARTMENT_ID = " + $('#ddlSelect').val() ; 
else 
    query = "SELECT EMPLOYEE_NAME from table_name"; 
+0

請添加一些關於你的答案的解釋。 –