Sql Query在特定日期内获取具有特定值的记录

时间:2016-04-28 03:21:40

标签: mysql asp.net

Can任何人都可以帮我编写一个查询,通过该查询我可以获取具有特定日期值的特定日期的记录。下面给出的这个查询给出了具有特定值但没有过滤日期的记录。

sql = "Select * from solarleads where Phone = '" + c_id.Text + "' OR AgentName Like '" + c_id.Text + "%' OR CallStatus Like '%" + c_id.Text + "%' OR CenterId = '" + c_id.Text + "' And Date >= '" + date1.Text + "' AND Date <='" + date2.Text + "' ORDER BY Id DESC ;";

2 个答案:

答案 0 :(得分:0)

您需要包含一组ORed条件。

sql = "Select * from solarleads 
where ( Phone = '" + c_id.Text + "' OR AgentName Like '" + c_id.Text + "%' 
OR CallStatus Like '%" + c_id.Text + "%' 
OR CenterId = '" + c_id.Text + "' ) 
And Date >= '" + date1.Text + "' AND Date <='" + date2.Text + "' 
ORDER BY Id DESC ;"

答案 1 :(得分:-1)

用括号分隔和条件,如 -

sql = "Select * from solarleads where " +
"(Phone = '" + c_id.Text + "' " +
  "OR AgentName Like '" + c_id.Text + "%' " +
  "OR CallStatus Like '%" + c_id.Text + "%' " +
  "OR CenterId = '" + c_id.Text + "' ) " +
"(And Date >= '" + date1.Text + "' AND Date <='" + date2.Text + "' )" +
"ORDER BY Id DESC ;";

同时检查 - Mysql or/and precedence?