Getting no output while passing String parameters in mysql query - mysql

In the given below these two queries the first query runs successfully and display the result but while running second query i am getting no output and no error.
So can any please explain why i am not getting output in second query.
String firstdate="2014-11-01";
String lastdate="2014-11-30";
//1 query
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN '2014-11-01' AND '2014-11-30' AND
reader_id="+Integer.parseInt("136"));
//2 query
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN "+firstdate+" AND "+lastdate+" AND
reader_id="+Integer.parseInt("136"));

Well when you doing second query and giving external parameters u need to do something like this.
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN '"+firstdate+"' AND '"+lastdate+"' AND
reader_id="+Integer.parseInt("136"));
tihs is what i think its should work.

Ideally, you should be using prepared statements in your queries, to avoid this kind of issues. For a tutorial on prepared statements(using java), check here
To solve your existing query,
You are not getting output in the second query because, on substitution of the variables, your query would be:
WHERE STARTTIME BETWEEN 2014-11-01 AND 2014-11-30
which is wrong and bound to throw exceptions.
Correct query should be
WHERE STARTTIME BETWEEN '2014-11-01' AND '2014-11-30'
which implies, you should change your text to
WHERE STARTTIME BETWEEN '"+firstdate+"' AND '"+lastdate+"'

Related

SQL Sum returns a false value

I have an issue with the SUM-Function in my MySQL Workbench. When I use the function, it returns a false value.
I´d like to SUM these three numbers:
56,03
35,59
54,35
The result should be 145,97, but its just 145 instead. I tried these different codes:
SELECT SUM(price) FROM table;
This one returns the value 145.
SELECT ROUND(SUM(price),2) FROM table;
The second one returns the value 145.00.
I was wondering whats wrong with the code, because I tried it in another DB that I have in my MySQL Workbench. Also tried it over the Terminal in both databases. In the other database the function works correctly.
Best thing to do would be to change your table so that it is a decimal field.
However, if you can't do that, the following query should get what you want:
SELECT SUM(replace(price,',', '.')) FROM tbl;
You can test in on SQLFiddle

JOOQ how query for between dates

I'm using jooq in my project and I need to query some data between two dates.
The sql query which produces right data is
select created_on from queue_token where created_on between '2015-07-16' and '2015-07-17' and token_queue_id=1;
the equivalent jooq query which i have written is below but doesn't give out the required result
create.select().from(com.database.tables.QueueToken.QUEUE_TOKEN)
.where(com.database.tables.QueueToken.QUEUE_TOKEN.TOKEN_QUEUE_ID.equal(1))
.and(com.database.tables.QueueToken.QUEUE_TOKEN.CREATED_ON.between(new Timestamp(fromDate.getTime())).and(new Timestamp(toDate.getTime())))
.fetch();
The jooq query produces result but only produces records that exactly match the fromDate. So basically it's not working for the date range.
Can somebody help here?
I think the problem is in passing a timestamp or date and time (I do not know java well). So instead sending e.x. "2015-07-16", you get "2015-07-16 12:55:00" or "1436187300".
Try debug the value of new Timestamp(fromDate.getTime()) first and if I'm right, try to convert it to a simple date without time.
To getting correct date value without time you can use:
Java 8 package java.time LocalDate https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
or lib Joda Time http://joda-time.sf.net/

Condition check with IN clause mysql

I have a string returned from a function "'aa','bb','cc',..."(the function uses GROUP_CONCAT). I want to use this as a condition in the IN clase of mysql.
SELECT name,class,comment
FROM vwstudent v
WHERE name IN (select func())
I want the query to act like
SELECT name,class,comment
FROM vwstudent v
WHERE name IN ('aa','bb','cc','dd',..)
I assume ('aa','bb','cc','dd',..) is acting as a whole string here and isn't generating any result. What can I do to run this query error less.
I'm not sure if it'll help. But you might be able to hack something together by using PREPARE and EXECUTE as described here: How To have Dynamic SQL in MySQL Stored Procedure
... but that's completely new to me. I've never used anything like that in MySQL.

MySQL using table columns in function to create alias to be used in sorting

It sounds more complicated than it actually is. Here is what I'm trying to do within the SELECT part:
SELECT TIMESTAMPADD(
UCASE(
SUBSTRING(offset_unit,1,CHAR_LENGTH(offset_unit)-1)
),1,'2003-01-02') as offset_date
offset_unit is a VARCHAR column in the database. It contains one of the following: "Hours","Minutes".
offset is an INT.
I am trying to convert the offset_unit to uppercase, after I have removed the last character ('s') so I can have a proper interval (MINUTE, HOUR...) so I can get a date that I can use in sorting afterwards, but MySQL keeps throwing an error. I have tested each step by adding one function at a time, and it only fails after I add TIMESTAMPADD. If I enter MINUTE manually then it works.
Any way to get this working?
Additional info: I am running this in CakePHP 1.3, in a find, within the 'fields' array, but that shouldn't be important.
this can be easily achived by using CASE WHEN clause as:
SELECT (CASE
WHEN offset_unit = 'HOURS'
THEN TIMESTAMPADD(HOUR,`offset`,'2003-01-02')
WHEN offset_unit = 'MINUTES'
THEN TIMESTAMPADD(MINUTE,`offset`,'2003-01-02')
END) AS offset_date
FROM my_table;
SEE SQLFIDDLE DEMO HERE
It doesn't work because TIMESTAMPADD does not take a string as the first argument, but a unit keyword, for example MINUTE. My guess is that you need to do this in two steps, first get the unit and then construct a query with the correct keyword.

Trying to use BETWEEN condition to return rows based on date but getting no results

I have a database containing a list of events, it is formatted something like this:
Datef Event Location Discipline
10/01/2012 MG Training Brooklands MG
I am running this query in order to get the results between certain dates:
SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012
The query runs correctly and I know that there are relevant results in the database, but I receive no results when running the query in phpmyadmin (I just get told "Your SQL query has been executed successfully").
I was wondering if anyone had any idea why this wouldn't be returning results?
Update:
Putting dates in quotes (e.g. SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012) kinda works but there's a bug.
I've certain dates doesn't work. e.g. SELECT * FROM events WHERE Discipline IN ('COMM') AND Datef BETWEEN '2012-02-01' AND '2012-02-29' shows no results, even though there is an event on 2010-02-01. Is this a peculiarity in the BETWEEN algorithm, or am I still getting my query wrong?
Without quotes or anything designating those values as dates, it will simply do the math on the integers you wrote.
In other words, you ask for
BETWEEN 01/01/2012 AND 31/01/2012
So you get 1/1=1, then 1/2012 which is almost 0.
Then you do 31/1=31, then 31/2012 which is also almost 0.
So it becomes
BETWEEN 0 and 0
Try wrapping your code strtotime(). MySQL and PHP use different formatting.
For some additional info, here are a couple of links:
http://www.richardlord.net/blog/dates-in-php-and-mysql
http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/
Your column Datef is of the wrong type and should be datatime
SELECT
*
FROM
`events`
WHERE
`Discipline` IN ('MG')
AND
`Datef` BETWEEN '2012-01-01' AND '2012-01-31'