Date format in SQL query - php

i have table:
Birthday
id | name | date
1 | aaa | 1990-03-02
2 | bbb | 1990-03-12
3 | ccc | 1990-03-25
4 | ddd | 1990-04-25
5 | eee | 1990-04-23
6 | fff | 1990-04-26
7 | ggg | 1990-04-12
How is the best way to SELECT all names where date is 1990-04-xx?

SELECT name FROM table WHERE date LIKE '1990-04%'
Some other answers here are assuming you're storing the date in a datefield, but I assumed by the way it was laid out in your question that it was just a string. Going on that assumption, I knew that using the LIKE operator would let me use a wildcard (the % sign) to search for anything with that year and month. That said, this query will match anything that starts with 1990-04 so there is a possibility with malformed data that you could get some incorrect data (e.g. a date is entered into the database like '1990-041-12')

If it's a DATE value I'd suggest checking if it's between the beginning and the end of the month. If you chop up the date using DATE_FORMAT or some other function you'll lose any chance of optimization:
SELECT name FROM myTable WHERE `date` BETWEEN '1990-04-01' AND '1990-04-30'
If it's a DATETIME value, do this instead to account for values like 4/30/1990 at 9PM:
SELECT name FROM myTable WHERE `date` >= '1990-04-01' AND `date` < '1990-05-01'

If date is a DATE or DATETIME or TIMESTAMP column and you want an index to have chances to be used:
SELECT name
FROM tableX
WHERE date >= '1990-04-01'
AND date < '1990-05-01' ;
An index on (date) would be good. Even better, an index on (date, name).

Related

This SQL cannot cross a 'year end' boundary

This SQL is called by a Smarty File designed to download an .xlsx of the output. It works well for all months EXCEPT the Dec. (end of year) report. Something is needed to get it to cross the year-end boundary but I can't figure what. ? Thanks.
SELECT user_id, cert_name, from_email, created_at, phone as role,
delivery, user_street, user_city, user_state, user_zip,
user_country_id, standard_fee, expedited_fee
FROM support_tickets
WHERE nature = 2
AND user_id IS NOT NULL
AND created_at BETWEEN '2012-12-05 04:00:01' and date(concat_ws('-', #year, #month + 1, 1))
ORDER BY created_at DESC
Use date arithmetic, not string concatenation.
If you try to add #month+1 when month is 12, you get month 13, not month 1 of the next year. There is no month 13 in calendars currently in use.
For your case I would recommend using the LAST_DAY() function that is built into MySQL. This returns the last date in the current month of its argument.
Example:
mysql> SELECT LAST_DAY('2020-12-05') AS last;
+------------+
| last |
+------------+
| 2020-12-31 |
+------------+
I suspect you want the last day of the same month.
If you really want the first day of the next month, use date arithmetic:
mysql> SELECT LAST_DAY('2020-12-05') + INTERVAL 1 DAY AS next;
+------------+
| next |
+------------+
| 2021-01-01 |
+------------+

Complex Queries range dates

mySQL dateTime range Query Issue
how get count of proceser in 2017 by same date like 2017-08-07
date | name
-----------------------
2017-08-31 | amr
-----------------------
2017-08-05 | ahmed
----------------- -----
2018-08-08 | moh
how get 2017-01-01 BETWEEN 2017-12-31
------------------------
count | date
-----------------------
2 | 2017
-----------------------
1 | 2018
SELECT count(*)
FROM item WHERE
date IN
( SELECT date
FROM item WHERE
(BETWEEN '2017-03-15' AND '2017-09-31'))
I couldn't find a duplicate for you, but I am sure there is one somewhere.
If you use GROUP BY and the DATE_FORMAT() function, you can COUNT() the occurrences in each group.
SELECT COUNT(*),DATE_FORMAT(`date`,'%Y') FROM `item` GROUP BY DATE_FORMAT(`date`,'%Y');
You can include a WHERE clause before GROUP BY if you need to omit certain ranges of time.
1, date is a reserved MySQL word. It would be best to name your column something else, but you can use it if you also specify the table name like table.date
2, there is no need for a sub-query. MySQL can do this in one query
SELECT count(`table.name`) as NUMBER, YEAR(`table.date`) as YR FROM item WHERE DATE(`table.date`) BETWEEN '2017-01-01' AND '2017-12-31'
BETWEEN is kind of a "ternary" operator, of the form x BETWEEN y AND z
You want:
`date` BETWEEN '2017-03-15' AND '2017-09-31'

convert timestamp to month in codeigniter [duplicate]

I have this certain problem about mysql date functions.
I'm trying to compare the value of THIS MONTH to the given timestamp in database.
For example, month today is june, and the timestamp is 1369967316
And I'm trying to determine if that timestamp is in month of june.
$query = db_query("SELECT * FROM users WHERE MONTH(FROM_UNIXTIME(CURDATE())) = MONTH(1369967316)");
//count total members this mont
$members_month = $query->rowCount();
so if I used the rowCount, the $members_month should have the value of 1.
Unfortunately it doesn't work.
Any help would be appreciated.
Well I saw some answers that some kind of relevant to mine but it doesn't hit the spot or I didn't applied it well.
mysql get month from timestamp not working
how to use curdate() in where clause against unixtimestamp (bigint) column
This works for me:
mysql> SELECT MONTH(FROM_UNIXTIME(1369967316));
+----------------------------------+
| MONTH(FROM_UNIXTIME(1369967316)) |
+----------------------------------+
| 5 |
+----------------------------------+
Your issue is likely coming from the fact that 1369967316 is May 30th, not June (as you expect), thus resulting in an inequality with MONTH(CURDATE()).
mysql> SELECT FROM_UNIXTIME(1369967316);
+---------------------------+
| FROM_UNIXTIME(1369967316) |
+---------------------------+
| 2013-05-30 22:28:36 |
+---------------------------+

MySQL: Get record where TIME('xx:yy:zz') between start and end time

This is the table:
+----+-----------+------------+----------+
| id | id_sensor | start_time | end_time |
+----+-----------+------------+----------+
| 1 | 12 | 21:15:00 | 02:45:00 |
| 2 | 7 | 00:00:00 | 23:15:00 |
| 3 | 5 | 04:30:00 | 16:30:00 |
+----+-----------+------------+----------+
I need to get record(s) where a specific time (e.g. 01:00:00) passed by PHP is between. start_time and end_time are TIME fields in UTC, I'm passing to the query hour via php, note, converted in php from user_timezone to UTC.
SELECT * FROM test WHERE TIME('01:00:00') BETWEEN start_time AND end_time;
Query returns only record id 2, not the 1. I need both, in this case (for id 1, end time ofcourse is next day).
Of course, if we looking for TIME('01:00:00'), we don't need the id 3.
Thank you.
I think this is the logic you want:
SELECT *
FROM test
WHERE (start_time < end_time AND TIME('01:00:00') BETWEEN start_time AND end_time) OR
(start_time > end_time AND TIME('01:00:00') NOT BETWEEN end_time AND start_time);
now() returns both, current time and date. You can also work with curtime(), which returns only the current time.
BTW, i think that working with SELECT * should be avoided (maybe you used it just for this example), it is IMHO always better to list the fields needed explicitly.

PHP MYSQL Detect if new row is added

Any Idea how can I identify if there is new client added on my database.
I was thinking about identifying it thru date_added field.
id client_name date_added
---------------------------------
1 ABC 2013-01-02
2 XYZ 2013-01-03
3 EFG 2013-01-02
4 HIJ 2013-01-05
as you can see a new client added HIJ on 2013-01-05.
I was looking with this kind of result:
Client List
Total NO: 4
New Client
Total No: 1
Client Name: HIJ
add a field new to the table, default it to 1, on page load use that for the select and set it to 0 to indicate its not longer new.
It's hard to tell but based on your comment ...my reference date is 1 month interval... you might be looking for something like this
SELECT id, client_name, new_count, total_count
FROM
(
SELECT id, client_name
FROM clients
WHERE date_added BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
) c CROSS JOIN
(
SELECT
(
SELECT COUNT(*) new_count
FROM clients
WHERE date_added BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
) new_count,
(
SELECT COUNT(*) total_count
FROM clients
) total_count
) t
Obviously you can easily change CURDATE() with any other reference date in the past in this query and you get results for that date.
Lets assume that you have following sample data
+------+-------------+------------+
| id | client_name | date_added |
+------+-------------+------------+
| 1 | ABC | 2013-05-13 |
| 2 | XYZ | 2013-06-13 |
| 3 | EFG | 2013-06-13 |
| 4 | HIJ | 2013-08-11 |
+------+-------------+------------+
and today is 2013-08-13 then the output from the query will be
+------+-------------+-----------+-------------+
| id | client_name | new_count | total_count |
+------+-------------+-----------+-------------+
| 4 | HIJ | 1 | 4 |
+------+-------------+-----------+-------------+
You could remember, in your webpage or PHP script, the highest ID value previously seen. Or the highest timestamp (better than a date) previously seen.
I prefer ID or Version numbers for concurrency-related stuff (locking, finding the latest etc) -- since they should be defined to be ascending, can't suffer "same millisecond" collisions, and are more efficient.
I assume you're going to hold the "state" of your application (as to what the user has seen) in hidden fields in the form, or somesuch. This would then track the "last seen" and allow you to identify "newly added" since the last pageview.
If you expect to identify newly added when coming from a different page or logging onto the application, you'll need to store the "state" in the database instead.
That depends on what you consider NEW. You have to define what you're going to compare the records against (reference date). Once you define it, you could use a query like the following:
SELECT * FROM client WHERE date_added >= '$date'
where $date is the reference date.

Categories