3

I Have a Table Name transactions

+-------+---------------------+--------+
| t_id  | date                | amount |
+-------+---------------------+--------+
|   10  | 2016-04-17 19:24:05 | 1800   |
|   12  | 2016-06-11 12:40:13 | 200    |
|   17  | 2016-04-13 14:42:04 | 100    |
|   19  | 2016-05-14 17:45:43 | 1      |
|   20  | 2016-08-15 19:45:54 | 999    |
|   21  | 2016-01-17 11:46:02 | 1500   |
|   41  | 2016-02-18 17:23:14 | 500    |
|   42  | 2016-07-19 13:26:14 | 500    |
|   43  | 2016-02-18 17:23:15 | 500    |
|   44  | 2016-02-18 17:23:16 | 500    |
|   45  | 2016-02-18 18:23:16 | 500    |
|   46  | 2016-02-18 17:23:16 | 500    |
|   47  | 2015-10-18 14:23:17 | 500    |
|   48  | 2015-11-18 17:23:17 | 500    |
|   49  | 2015-12-18 11:23:18 | 500    |
|   50  | 2015-05-18 11:25:54 | 1000   |
|   51  | 2015-09-18 12:26:22 | 3000   |
|   52  | 2015-05-18 13:48:59 | 10     |
|   53  | 2015-03-18 15:48:59 | 10     |
|   54  | 2015-01-18 17:49:13 | 5000   |
+-------+---------------------+--------+

I want to SELECT record with date

I Use

SELECT * FROM transactions WHERE date='2016-02-18';

I Also SELECT between 2 date And I Use

SELECT * FROM transactions WHERE date<'2016-02-18' AND date>'2016-02-01';

But Its Not Working.(I use php Mysql xampp) Can you help me understand the concepts?

3
  • What do you mean by "it's not working"? Are you getting an error message? Is it returning the wrong data? Commented Feb 18, 2016 at 13:35
  • 2
    What is the date column's type? timestamp/datetime or string? Commented Feb 18, 2016 at 13:37
  • Also, amount looks distressingly like a string :-( Commented Feb 18, 2016 at 13:56

4 Answers 4

2

For the ist query you need to use DATE() function becuase your column type is DATETIME or TIMESTAMP so you can handle it as:

SELECT * FROM transactions WHERE DATE(date) = '2016-02-18';

For second query you can simply add the TIME as:

SELECT * FROM transactions WHERE date > '2016-02-01 00:00:00' AND date < '2016-02-18 23:59:59';
Sign up to request clarification or add additional context in comments.

3 Comments

Are you his column is a DATETIME column? The OP never specifies that, even though the question was asked. :)
@jay-blanchard hello sir.. Yes u r 100% right. But in value I just saw the 24hours format and guess its DATE AND TIME.. mmaybe DATETIME or TIMESTAMP
How Can I Select Current or last month all transactions. Because All Months are not 30 days. So Its a big problem for me.
1

Is it a mysql DB ?

If it is than use DATE function :

SELECT * FROM transactions WHERE DATE(date)='2016-02-18';

OR

SELECT * FROM transactions WHERE date BETWEEN '2016-02-18' AND '2016-02-01';

2 Comments

How Can I Select Current or last month all transactions. Because All Months are not 30 days. So Its a big problem for me.
Sorry, I'm not sure to understand what you want. Maybe you should take an eye at mysql documentation to solve it : Mysql Date and Time functions. Or rephrase your need. (Or maybe you just need to use "MONTH()" function ?)
1

use this

use % and like in your query . you will get all results in given date

SELECT * FROM transactions WHERE date like '2016-02-18%';

3 Comments

Why should the OP "use this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
While you made edits you didn't explain why the OP should use % and LIKE. You just told them to use it without any context. :)
How Can I Select Current or last month all transactions. Because All Months are not 30 days. So Its a big problem for me.
0

you should use BETWEEN ... AND ...

SELECT * FROM transactions WHERE `date` BETWEEN '2016-02-01' AND '2016-02-18';

4 Comments

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
How Can I Select Current or last month all transactions. Because All Months are not 30 days. So Its a big problem for me.
@JayBlanchard where it is write you should not write 'try this' in answer. i have seen many user which have 300k and he write 'Try This' and 'use This' but no one can interfere so why you said. and you can comment on post but if my query is right so you haven't rights to down vote it. sorry if you hurt.
I'm not hurt and if I saw a 300k user do the same thing I would make the same comment. Answers with explanations are always better. They server to teach not only the OP but others who will be reading the answer. My comment is not meant as interference, it is meant as guidance. It is your choice what you do with it. I did not down vote your answer, someone else did. I never down vote a valid answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.