1

我正在通过 java 查询 mysql,我得到了输出。

中间,我有

WHERE 
    ipaddress = 'moteid6'
AND datetime BETWEEN '2012-09-25 15:45:00' AND '2012-09-25 18:45:00'

但是当我给 datetime1 而不是 2012-09-25 15:45:00 和 datetime2 而不是 2012-09-25 18:45:00 和 moteid 而不是 moteid6 就像用户输入一样,

System.out.println("Enter datetime1 in the format YY-MM-DD HH:MM:SS");
Scanner keyboard1 = new Scanner(System.in);
datetime1 = keyboard1.nextLine();
System.out.println("Enter datetime2 in the format YY-MM-DD HH:MM:SS");
Scanner keyboard2 = new Scanner(System.in);
datetime2 = keyboard2.nextLine();
System.out.println("Enter moteid");
Scanner keyboard3 = new Scanner(System.in);
datetime2 = keyboard3.nextLine();

然后通过替换 datetime1 而不是 2012-09-25 15:45:00 来查询 mysql,同样是 datetime2 和 moteid 而不是 moteid6,它正在输入但不查询 mysql 我得到的结果是

run:
Error: null
Enter datetime1 in the format YY-MM-DD HH:MM:SS
2012-09-25 15:45:00
Enter datetime2 in the format YY-MM-DD HH:MM:SS
2012-09-25 18:45:00
Enter moteid
moteid6
BUILD SUCCESSFUL (total time: 30 seconds)
The query i'm giving is 
String sql = ("SELECT (b.l2framessent - a.l2framessent) AS netl2framessent,CONVERT(endOfInterval*500, DATETIME) endOfInterval FROM (SELECT datetime DIV 500 startOfInterval,l2framessent FROM Statistics3,(SELECT MIN(datetime) mindate FROM Statistics3 WHERE ipaddress='moteid' AND datetime BETWEEN 'datetime1' AND 'datetime2' GROUP BY datetime DIV 500) b WHERE ipaddress='moteid' AND datetime = mindate) a, (SELECT datetime DIV 500 endOfInterval, l2framessent FROM Statistics3, (SELECT MIN(datetime) mindate FROM Statistics3 WHERE ipaddress = 'moteid' AND datetime BETWEEN 'datetime1' AND 'datetime2' GROUP BY datetime DIV 500) b WHERE ipaddress = 'moteid'AND datetime = mindate) b WHERE endOfInterval = startOfInterval+ 1 ");
4

1 回答 1

0

如果我的问题正确,您想在选择查询中传递变量名称,如下所示:

     WHERE 
    ipaddress = 'moteid6 '
AND datetime BETWEEN 'datetime1' AND 'datetime2'

您必须将日期和字符串放在引号内。

我试图通过整个查询,但这似乎是错误的

   "(SELECT (b.l2framessent - a.l2framessent) AS netl2framessent,CONVERT(endOfInterval*500, DATETIME) 
endOfInterval FROM (SELECT datetime DIV 500 startOfInterval,l2framessent FROM Statistics3,
(SELECT MIN(datetime) mindate FROM Statistics3 WHERE ipaddress='moteid' AND datetime BETWEEN '"+datetime1+"' AND '"+datetime2+"' 
GROUP BY datetime DIV 500) b WHERE ipaddress='"+moteid+"' AND datetime = mindate) a, 
(SELECT datetime DIV 500 endOfInterval, l2framessent FROM Statistics3, (SELECT MIN(datetime) mindate FROM Statistics3 WHERE 
ipaddress = '"+moteid+"' AND datetime BETWEEN '"+datetime1+"' AND '"+datetime2+"' GROUP BY datetime DIV 500) b WHERE 
ipaddress = '"+moteid+"'AND datetime = mindate) b WHERE endOfInterval = startOfInterval+ 1 ");
于 2012-10-02T08:36:47.630 回答