0
String sql="SELECT a.ID,a.VISITOR_TEXT,a.COMPANY_TEXT,a.CONTACT,a.PERSON_TO_MEET,a.DEPARTMENT_TEXT,a.FLOOR,b.BELONGING_TYPE,b.BELONGING_TEXT ,a.INTIME, c.EXIT_TIME FROM tm_visitor a  LEFT JOIN tm_belonging b ON b.BID=a.ID LEFT JOIN tm_exit c ON c.ID=a.ID limit "+i+",10";

System.out.println(sql);
ResultSet rs=st.executeQuery(sql);
while(rs.next())
    {
    ar.add(rs.getInt("ID"));
    ar.add(rs.getObject("VISITOR_TEXT"));
    ar.add(rs.getObject("COMPANY_TEXT"));
    ar.add(rs.getObject("CONTACT"));
    ar.add(rs.getObject("PERSON_TO_MEET"));
    ar.add(rs.getObject("DEPARTMENT_TEXT"));
    ar.add(rs.getObject("FLOOR"));
    ar.add(rs.getObject("BELONGING_TYPE"));
    ar.add(rs.getObject("BELONGING_TEXT"));
    ar.add(rs.getObject("INTIME"));
    ar.add(rs.getObject("EXIT_TIME"));

    }

<tr>
    <td align="center"><input type="text" name="id" value="<%=itr.next()%>" readonly size="15"></td>
    <td align="center"><input type="text" name="visitorname" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="company" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="contact" value="<%=itr.next()%>" readonly size="10"></td>
    <td align="center"><input type="text" name="whometomeet" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="department" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="floor" value="<%=itr.next()%>" readonly size="7"></td>
    <td align="center"><input type="text" name="btype" value="<%=itr.next()%>" readonly size="15"></td>
    <td align="center"><input type="text" name="bel" value="<%=itr.next()%>" readonly size="15"></td>
    <td align="center"><input type="text" name="intime" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="exittime" value="<%=itr.next()%>" readonly size="20"></td>



</tr>

执行上述查询后,如果 tm_exit.EXIT_TIME 中的值不存在,则在 JSP 表文本框中显示 NULL 我该如何避免这种情况

我试过了:

<%!
String blanknull(String s) {
  return (s == null) ? "" : s;
}
%>

但没有用,请帮忙?

这是我用来获取数据的查询

SELECT a.ID,a.VISITOR_TEXT,a.COMPANY_TEXT,a.CONTACT,a.PERSON_TO_MEET,a.DEPARTMENT_TEXT,‌​a.FLOOR,b.BELONG ING_TYPE,b.BELONGING_TEXT ,a.INTIME, c.(ifnull(EXIT_TIME,'NA')) as EXIT_TIME FROM tm_visitor a LEFT JOIN tm_b elonging b ON b.BID=a.ID LEFT JOIN tm_exit c ON c.ID=a.ID; 

但给出以下错误

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 '(ifnull(EXIT_TIME,'NA')) 作为 EXIT_TIME FROM tm_visitor a LEFT JOI N tm_belonging 附近使用的正确语法

4

1 回答 1

0

你可以避免空值,使用 MySQL 的 ifnull 函数,

具有空值的表:

mysql> select * from test;
+------+---------+
| id   | name    |
+------+---------+
|    1 | Jcho360 |
|    1 | NULL    |
+------+---------+
2 rows in set (0.00 sec)

带有 IFNULL 函数的表

mysql> select id, ifnull(name,'Its NOT NULL')as name from test;
+------+--------------+
| id   | name         |
+------+--------------+
|    1 | Jcho360      |
|    1 | Its NOT NULL |
+------+--------------+
2 rows in set (0.00 sec)

而不是 'its NOT NULL' 你可以放 ' ' 或其他任何东西,你不会再有 null

编辑:

尝试

String sql="SELECT a.ID,a.VISITOR_TEXT,a.COMPANY_TEXT,a.CONTACT,a.PERSON_TO_MEET,
a.DEPARTMENT_TEXT,a.FLOOR,b.BELONGING_TYPE,b.BELONGING_TEXT ,a.INTIME,
 ifnull(c.EXIT_TIME,0) FROM tm_visitor a  
LEFT JOIN tm_belonging b ON b.BID=a.ID 
LEFT JOIN tm_exit c ON c.ID=a.ID limit "+i+",10";
于 2012-06-11T19:50:32.773 回答