【参加讨论】我们到目前为止所谈到的sql语句相对较为简单,如果再能通过标准的recordset循环查询,那么这些语句也能满足一些更复杂的要求。不过,何必非要拘泥在浅尝则止的基础水准之上呢?你完全可以再增加其他一些符号,比如and、 or和not来完成更强大的功能。 以下面的sql语句为例:
sql ="select c_firstname, c_lastname, c_email from customers where c_email is
not null and c_purchase = '1' or c_purchase = '2' and c_lastname like
'a%'"
就你目前所掌握的sql知识,以上的例子也不难解释,不过上面的语句并没有很明白地让你看清条件字句是如何胶合在单一sql语句中的。
多行语句
在sql语句不好懂的情况下,你不妨把整个语句分解为多行代码,然后在现有变量基础上逐步增加查询语句的各个组成部分并把它存在同一变量内:
sql = "select c_firstname, c_lastname, c_emailaddress, c_phone"
sql = sql & " from customers"
sql = sql & " where c_firstname like 'a%' and c_emailaddress not null"
sql = sql & " order by c_lastname, c_firstname"
到了最后一句,sql变量就包含了以下的完整select 语句:
"select c_firstname, c_lastname, c_emailaddress, c_phone from customers
where c_firstname like 'a%' and c_emailaddress no null order by c_lastname,
c_firstname"
整句照上面分解之后显然好读多了!在进行调试的时候,你或许更乐于多敲几个字符把程序改得更好读些。不过你可要记住了,在封闭引号之前或者在打开引号之后你需要增加空格,这样才能保证字符串连接起来的时候你没有把几个词凑到了一块。