Unable to get Results of a query - mysql

I am newbie to SQL.
I have this query
I am not sure where i am doing it wrong.
Can you guys help me out?
thanks.

Three mistakes:
You need single quotes
MySql expects two-digit months
The 2015-09-30 end date includes an implicit midnight for the time component, which excludes most of the day
Put it all together you get this:
SELECT * FROM results WHERE played_on BETWEEN '2015-09-01' AND '2015-10-01';
While I'm here, I prefer to avoid BETWEEN in favor of explicit bounds. There's always that chance someone codes a game for exactly midnight October 1:
SELECT * FROM results WHERE played_on >= '2015-09-01' AND played_on < '2015-10-01';
And you would have had this answer faster if I could have copy/pasted the query text from your post instead of having to re-type. Posting images of sample data or code instead of the text is considered very rude here.

i think you can use MySql Date Function like MONTH() and YEAR() for this query :
SELECT * FROM results WHERE MONTH(played_on) = 9 AND YEAR(played_on) = 2015
I hope this answer can help you.

Related

MySQL query - check between two dates without needing to retrieve rows

I have a table that includes dates, I'm trying to check if a date I have falls between the dates in the table. My query is working, but it doesn't return anything. This seems like it should be very simple, but I can't wrap my head around it.
SQL query looks like this:
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
The dates are generated dynamically in my script so I can ascertain if what I'm passing into it falls between the beginning and end dates in my table. I don't need any specific data from the table, just a boolean telling me whether the date is between the beginning and end dates or not.
You are looking for EXISTS:
SELECT
EXISTS(
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
) AS hasValue
Hope this helps,
Check your MySQL server's date format and your generated date format.
MySQL date format is like that: '2019-01-30 18:19:52'
Also you can try change
(beginning_date_from_table) and (end_date_from_table)
to
(end_date_from_table) and (beginning_date_from_table)
Check: How do I query between two dates using MySQL?
I phrased the question slightly wrong, in that I was trying to return the result of a conditional. Once I realised how to ask google the right thing, I quickly came up with this solution:
SELECT (CASE WHEN this_date BETWEEN beginning_date AND end_date THEN 1 ELSE 0 END) AS date_result
Thanks to the other people who answered, your input put my thinking on the right track.

mysql query not returning results

I am executing this query
SELECT *
FROM temp
WHERE DATE_FORMAT(startTime,'%m/%d/%Y') = '7/15/2012'
and startTime column has this value '2012-07-15 12:00:00'
But this is not returning any results. Can somebody please help?
Change here:
7/15/2012
to:
07/15/2012
According to the documentation for the DATE_FORMAT function, %m is "Month, numeric (00..12)". Note the zero-padding. So you need to write '07/15/2012' rather than '7/15/2012'.
(And in case you're wondering — I have no idea what month #0 is. So far as I'm aware, the months range from 01 to 12. Maybe some locales do have a month #0?)

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'

How can I resolve this issue using an sql query

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in Layman's expression ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
Remove the quotes around the field names
SELECT
DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) AS cap_maturity FROM capital
I think this should work.
I know its is an old question, but just trying to help others

mysql calculation

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in layman's term ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
It sounds like you want this:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR)
...
This DATE_ADD function adds the given number of years to the starting date, thus producing the maturity date — just as you've described.
Please show the errors you get. The query
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) FROM capital
should work.