在BETWEEN不能正常工作之后使用OR条件

时间:2017-05-12 06:18:47

标签: mysql

我的SQL代码有 if (Directory.Exists(FileNamePath)) { List<string> dbFileNames = null; string qry = "select FileName from MembersFiles where fk_MemberId='" + GlobalValues.Member_PkId + "' and Document_Type='" + doc + "'"; using (SqlDataAdapter da = new SqlDataAdapter(qry, con)) { con.Open(); DataTable dt = new DataTable(); da.Fill(dt); dbFileName = dt.Rows.OfType<DataRow>() .Select(r => r[0].ToString()).ToList(); con.Close(); } string[] fileNames1 = dbFileNames.Select(f => Path.Combine(FileNamePath, f)).ToArray(); } BETWEEN条件。

OR

上面的代码抛出错误,

SELECT * FROM tbl_order o INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id LEFT JOIN tbl_title t ON t.title_id = c.title_id LEFT JOIN tbl_assign a ON (a.order_id = o.order_id AND a.order_no_first = o.order_no_first) WHERE o.order_status = 1 AND o.order_date BETWEEN DATE(DATE_ADD(o.order_date, INTERVAL -2 DAY) AND CURDATE()) OR o.order_print = 1 GROUP BY o.order_id

如果使用#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR o.order_print = 1 GROUP BY o.order_id LIMIT 0, 25' at line 1代替AND,则代码可以正常运行。但我需要OR而不是OR

如何解决此错误。我哪里做错了?

1 个答案:

答案 0 :(得分:4)

你混淆了括号:

o.order_date BETWEEN DATE(DATE_ADD(o.order_date, INTERVAL -2 DAY) AND CURDATE())

应该是:

o.order_date BETWEEN DATE(DATE_ADD(o.order_date, INTERVAL -2 DAY)) AND CURDATE()

BETWEEN需要两个参数,并且两个参数周围不能有括号。

像这样:

o.order_date BETWEEN X AND Y  

不喜欢这样:

o.order_date BETWEEN (X AND Y)