我正在通过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 ");
答案 0 :(得分: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 ");