i've a mysql table like this:
| ID | ID_period | date_start | date_end |
| 1 | 1 | 0000-07-01 | 0000-08-31 |
| 2 | 2 | 0000-09-01 | 0000-10-30 |
| 3 | 3 | 0000-11-01 | 0000-12-28 |
| 4 | 4 | 0000-11-01 | 0000-03-31 |
how can i select IDs that are included between this period 0000/07/14 - 0000/08/25 ?
date_start and date_end columns are DATE format.
THE PROBLEM is that if i search for a period (included and intersect) that is ie: from 0000-12-12 to 0000-01-25 i get 0 records from the select, i guess for the year that is '0000'.. how can i fix it ?
another problem is that if i search a period like 11-01 to 12-31 i got 0 results.. because last day od december in date_end is 28.. but if i search for a period 11-01 to 12-31 is because i want all the records included.. so i'd like to get the record having id=3 and id=4
at the moment im using the following query:
SELECT ... WHERE '12' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '15' BETWEEN DAY(date_start) and DAY(date_end)
AND '03' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '28' BETWEEN DAY(date_start) and DAY(date_end)
SELECT *
FROM mytable
WHERE date_start BETWEEN '0000-07-14' AND '0000-08-25'
OR date_end BETWEEN '0000-07-14' AND '0000-08-25'
OR (date_start<'0000-07-14' AND date_end>'0000-08-25')
Related
I have a table prices with where I store more times in a day more values referred to a customer like this:
Table prices:
| id | customer_id | value_1 | value_2 | value_3 | created_at |
-------------------------------------------------------------------------
| 1 | 10 | 12345 | 122 | 10 | 2021-08-11 10:12:40 |
| 2 | 10 | 22222 | 222 | 22 | 2021-08-11 23:56:20 |
| 3 | 12 | 44444 | 444 | 44 | 2021-08-12 08:12:10 |
| 4 | 10 | 55555 | 555 | 55 | 2021-08-13 14:11:20 |
| 5 | 10 | 66666 | 666 | 66 | 2021-08-13 15:15:30 |
| 6 | 10 | 77777 | 777 | 77 | 2021-08-13 16:12:50 |
I have some filters on that table to retrieve only records with date greater than X and/or lower than Y, sort records by value_1 or value_2, etc...
With that filters I have to take only 1 record for each day of a customer specified.
I'm able to get the record with the highest value_1 for example, by using sql function max() and group by date.
// Init query
$query = Price::query();
// Take the greatest value of value1
$query = $query->selectRaw(
'max(value_1) as highest_value_1, ' .
'date(created_at) as date'
);
// If defined, add a greater or equals
if ($from) $query->where("created_at", ">=", $from);
// If defined add a lower or equals
if ($to) $query->where("created_at", "<=", $to);
// Get results for current customer only, grupping by date and ordering it
$query = $query->where('customer_id', $id)->groupBy('date')
->orderBy('date', 'DESC');
// Fetch records
$records = $query->get();
But now I would like to have only the last record for each day of a customer specified.
I need an eloquent/sql solution because the date range to search may be large and the table has a lot of records.
How can I archive that?
Thanks
Not a complete solution (no customer filter nor laravel use), but I would use something like that in pure sql :
SELECT
CONCAT(EXTRACT(YEAR_MONTH FROM created_at),EXTRACT(DAY FROM created_at)) AS day,
MAX(created_at)
FROM mytable
GROUP BY day;
Of course, you may use other function to group by day (like string regexp or substr).
Okay guys I have big problem with mySQL. I need to to do select that gets the free tables in the restaurant in specific time. Here are my tables:
tables:
+-------------+
| id | chairs|
+-------------+
| 1 | 3 |
| 2 | 5 |
| 4 | 10 |
| 7 | 12 |
| 10 | 6 |
+-------------+
and reservations:
+---------------------------------+
| id | table_id | start_datetime |
+----------------|---------------------+
| 1 | 3 | 2013-11-27 16:15:00 |
| 2 | 5 | 2013-11-27 19:00:00 |
+----------------|---------------------+
I try something like this
SELECT *
FROM wp_reserveit_tables,wp_reserveit_reservations
WHERE (wp_reserveit_tables.chairs = wp_reserveit_reservations.table_id
AND wp_reserveit_reservations.start_datetime - INTERVAL 1 HOUR >= '2013-11-29 16:15:00'
AND wp_reserveit_reservations.start_datetime + INTERVAL 1 HOUR <= '2013-11-29 16:15:00')
OR wp_reserveit_tables.chairs <> wp_reserveit_reservations.table_id
but it gives me SQL error.
So please if you have and idea please write it down.
Thanks
Reservation table will have entry only if there are any reservations. So we can do it simply with NOT IN clause I guess,
SELECT * FROM wp_reserveit_tables WHERE chairs_id NOT IN
(SELECT table_id FROM wp_reserveit_reservations GROUP BY table_id)
In your query you have joined them with chairs column with table_id column. So am not sure which one matches. So please alter the column in Where clause as per your requirement.
You have column name start_datetime in your query and your table structure specifies it to be starttime.
Unless you specify further, this does look like a valid error if it is not a typo.
I can code in PHP but I'm not good with SQL at all. I need to run an update on a table in order to pass in a given user_id and set the "access_end" date for all products the user owns to one year from today's date.
Any help much appreciated
Database is MySQL
Table name is dap_users_products_jn
Relevant Fields in database are:
user_id | access_end_date | product_id
1 | 2012-10-26 | 34
1 | 2012-11-21 | 30
1 | 2012-12-22 | 3
2 | 2012-10-20 | 34
2 | 2012-07-18 | 30
2 | 2012-08-15 | 3
...etc
update dap_users_products_jn
set access_end_date = date_add(now(), interval 1 year)
where user_id = 1
I am creating a graph where I can get the total views everyday for a certain range, or as long it goes back.
The problem I am having is to fill a default number of 0 when no views has been made for a certain day, some days there may be absolutely no views in a day so I need MySQL to return a default of 0 when none is found - I have no idea how to do this.
This is the query I use to get the total views a day:
SELECT DATE(FROM_UNIXTIME(v.date)) AS date_views,
COUNT(v.view_id) AS total_views
FROM
(
views v
)
GROUP BY date_views
ORDER BY v.date DESC
My results return this:
+------------+-------------+
| date_views | total_views |
+------------+-------------+
| 2012-10-17 | 2 |
| 2012-10-15 | 5 |
| 2012-10-14 | 1 |
| 2012-10-10 | 7 |
+------------+-------------+
However there are missing days that I want to return 0 for it, as 2012-10-16, 2012-10-11, 2012-10-12, 2012-10-13 is not included.
So, for example:
+------------+-------------+
| date_views | total_views |
+------------+-------------+
| 2012-10-17 | 2 |
| 2012-10-16 | 0 |
| 2012-10-15 | 5 |
| 2012-10-14 | 1 |
| 2012-10-13 | 0 |
| 2012-10-12 | 0 |
| 2012-10-11 | 0 |
| 2012-10-10 | 7 |
+------------+-------------+
Would be returned.
How would this be approached?
When I did this a couple of years ago I created an empty array with the date as key and the default value 0. Then I simply looped through the result att changed the value for those dates I had.
for each($result as $row){
$date_stats_array[$row['date']] = $row['value'];
}
In situations like this I create a temporary table which I fill with all the dates you want. After that, you can use that table to join your original query against.
To fill the table you can use this procedure:
DROP PROCEDURE IF EXISTS filldates;
DELIMITER |
CREATE PROCEDURE filldates(dateStart DATE, dateEnd DATE)
BEGIN
WHILE dateStart <= dateEnd DO
INSERT INTO tablename (_date) VALUES (dateStart);
SET dateStart = date_add(dateStart, INTERVAL 1 DAY);
END WHILE;
END;
|
DELIMITER ;
CALL filldates('2011-01-01','2011-12-31');
Courtesy of https://stackoverflow.com/a/10132142/375087
I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.