0

Can Any one help me to write a query by which i can fetch a record with specific date having specific value of a column. this query given below giving records with specific value but not filtering the date.

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 Answers2

0

You need to bracket the set of ORed conditions.

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 ;"
user5631
  • 5
  • 2
-1

Separate and condition by parenthesis, like -

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 ;";

Also check - Mysql or/and precedence?

Community
  • 1
  • 1
dibakar
  • 19
  • 6
  • you make SQL injection and is very insecure to use it. Never use `+` to make SQL strings in your application! – H. Pauwelyn Apr 28 '16 at 16:47
  • I am not sure why there is negative vote. OP provided a SQL query where logic was not correct and I fixed it. This is not a solution that I proposed. If he wants to use injection then vote him down, why me. As far as I am concerned it exactly solves his problem. – dibakar Apr 29 '16 at 16:06