I have two tables in my database. In the first table is a column named date where i insert a date period in a 1 day interval. In the second table are the calendar weeks of the same date period and an autoincrement column weekid.
For example I have the calendar week 25 with the weekid 145 (saved in the second table). The date area is from 21.06-27.06 and is saved in the first table.
Now i want to insert the weekid into the first table for every day (date) matching the calendar weeks.
Here are my tables:
CREATE TABLE `day` (
`dayid` int(255) NOT NULL AUTO_INCREMENT,
`userid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`dayid`)
CREATE TABLE `week` (
`weekid` int(255) NOT NULL AUTO_INCREMENT,
`userid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`calendar week` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` year(4) NOT NULL,
PRIMARY KEY (`weekid`
Example output for table "day":
weekid: 145
userid: 589
date: 2021-05-06
Example output for table "week":
weekid: 145
userid: 589
calendar week: 25
year: 2021
Does anyone have an idea how to do the date comparison?
If in the second table you don't have the dates of each week you can just calculate it on the first table right?
SELECT (EXTRACT(DOY FROM date_in_table1)/7)
should produce the work week of a certain date.
Related
I have the following table in my database.
CREATE TABLE `market_messages` (
`pm_id` int(11) NOT NULL,
`pm_item_index` int(11) DEFAULT NULL,
`pm_item_id` varchar(11) COLLATE utf8_polish_ci NOT NULL,
`pm_item_sender` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_client` varchar(30) COLLATE utf8_polish_ci NOT NULL,
`pm_item_sender_id` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_client_id` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_name` varchar(80) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_sender` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_receiver` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_entry_start` timestamp NULL DEFAULT NULL,
`pm_message` text COLLATE utf8_polish_ci DEFAULT NULL,
`pm_index` int(11) DEFAULT NULL,
`pm_type` varchar(10) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_user_offer` varchar(50) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_status` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
Now I'm trying to group messages and order in specific sequence on the list.
I need to list comments based on last entry into m_entry_start column which type is timestamp.
Here is my query, but result is not good:
$sql = "SELECT *
FROM market_messages
WHERE (pm_item_client = '$user_name'
AND pm_item_client_id = '$user_id')
OR (pm_item_sender = '$user_name'
AND pm_item_sender_id = '$user_id')
GROUP BY pm_item_id, pm_item_client_id, pm_item_sender_id
ORDER BY MAX(pm_entry_start) DESC;";
Result is not this what I have expected.
Echo $fetch['m_entry_start']; doesn't give me right result.
I'm expecting max value of pm_entry_start timestamp from each group, but in results is taken probably first value. Additionally `pm_entry_start' is echoed with ago() function to show elapsed time. Ago function is converting timestamp in format 2021-10-01 10:00:19 to something like 3 minutes ago, or 1 day ago etc.
This is example result:
$user_name
ago(timestamp)
Adam
1 month ago
Eve
23 days ago
Terese
2 months ago
Mark
5 days ago
Monica
3 min ago
I expecting results in order based on timestamp in descending order so from the last entry to the first.
$user_name
ago(timestamp)
Monica
3 min ago
Mark
5 days ago
Eve
23 days ago
Adam
1 month ago
Terese
2 months ago
How to write the good query? I have started programming in PHP last year. This project is my first with use of MySQL database. Any help appreciated.
Finally I have found the solution:
SELECT ..., MAX(pm_entry_start) AS pm_entry_start FROM ... was missing part of MySQL query.
Before I have tried only:
SELECT ..., MAX(pm_entry_start) FROM ...
How can I get the rows from table articles for the last 7 days?
Each row has a value timestmp where time is set via time().
I've tried this:
SELECT COUNT(*) FROM `articles` WHERE `timestmp`>NOW()-INTERVAL 168 HOUR
It doesn't work for me :(
The table is:
CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`link` text NOT NULL,
`article_name` text NOT NULL,
`descript` text NOT NULL,
`preview` text NOT NULL,
`content` text NOT NULL,
`category` int(11) NOT NULL,
`author` text NOT NULL,
`keywrds` text NOT NULL,
`timestmp` int(11) NOT NULL,
`modified` int(11) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT (`keywrds`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
The expected output is all the articles for the last 7 days with names, descriptions and so on.
Your timestmp column should be storing a UNIX timestamp, which is the number of seconds since the start of the UNIX epoch in January 1, 1970. So, if you just want records which happened exactly within the last 7 days, then you may just subtract 7 days (as seconds) from your timestmp column:
SELECT COUNT(*) AS cnt
FROM articles
WHERE timestmp > UNIX_TIMESTAMP() - 7*24*60*60;
If instead, you want records from 7 days ago, including the entire first day, then we need to do more work. In this case, we have to compute midnight on the first day, then convert that to a UNIX timestamp.
SELECT COUNT(*) AS cnt
FROM articles
WHERE timestmp > UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 7 DAY))
I have table:
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`amount` decimal(10,2) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
I need to have in result:
Total amount order by weekdays.
Email: test#mail.ru
Monday : 100
Tuesday : 33
Wednesday : 3461
Thursday: 0
Friday : 238
Saturday : 746
Sunday : 74
.....
Please help me to build query)
Here is a SQL query to solve your problem:
SELECT DAYNAME(created_date) as weekd, count(id) as cnt
FROM yourTable
WHERE created_date BETWEEN '2017-07-01 00:00:00' AND '2017-08-01 00:00:00'
GROUP BY weekd
The DAYNAME() function defines the names of the day of the week. Using GROUP BY will group by the day names. At the same time using COUNT() is the number of orders for each name of the day of the week.
SELECT SUM(amount)
FROM (
SELECT amount FROM table ORDER BY weekend
);
I am having trouble in getting the all the savings in a financial year. the financial year starts from April of this year to March of next year.
Here is the table structure
CREATE TABLE IF NOT EXISTS `savings` (
`id` int(11) NOT NULL,
`account_no` int(11) NOT NULL,
`financial_year` int(11) NOT NULL,
`reciept_date` date NOT NULL,
`data_entry_date` date NOT NULL,
`savings_reciept_no` int(11) NOT NULL,
`amount` int(10) NOT NULL,
`starting_balance` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=621 DEFAULT CHARSET=latin1;
Here is the sqlfiddle link http://www.sqlfiddle.com/#!9/bee53/18
What i'm missing in the query?
Is there any better query of getting the results in a financial year?
So, i'm trying to query all the data between 2 dates. The date field on my mysql table is at this format: (dd-mm-yyy). So, i'm using php for this.
I made something Like this:
SELECT *
FROM `sells`
WHERE date
BETWEEN '01-01-2013'
AND '04-02-2014'
of course this dates will be variables, but for test on phpmyadmin i didnt achieve the expected result. It gives me all the data ON this dates, let me show.
03-01-2014
03-01-2014
03-01-2014
01-02-2014
01-02-2014
03-02-2014
03-02-2014
03-02-2014
03-02-2014
And of course, all the other datas, but its irrelevant.
I want to bring all the data between a period of dates, how can i fix this error?
Thanks.
Added show table.
Its on my native language, so. (its a domestic system, for my home business
CREATE TABLE `vendas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_cliente` int(11) NOT NULL,
`cliente` varchar(255) CHARACTER SET latin1 NOT NULL,
`produto` varchar(255) CHARACTER SET latin1 NOT NULL,
`quantidade` int(255) NOT NULL,
`valor` varchar(255) CHARACTER SET latin1 NOT NULL,
`total` varchar(255) CHARACTER SET latin1 NOT NULL,
`metodo` varchar(255) CHARACTER SET latin1 NOT NULL,
`data` varchar(255) CHARACTER SET latin1 NOT NULL,
`status` int(255) NOT NULL DEFAULT '1',
`data_pagamento` varchar(255) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci
This is what happens when you store your dates as strings instead of as dates. You get query hell.
You will need to convert your string dates into the proper format and then change your search criteria to also be in the proper date format (YYYY-MM-DD):
WHERE STR_TO_DATE(date, "%d-%m-%Y")
BETWEEN '2013-01-01'
AND '2014-02-04'
Try this -
SELECT *
FROM `sells`
WHERE DATE_FORMAT(<your column name>,'%m-%d-%Y') >= '01-01-2013'
and DATE_FORMAT(<your column name>,'%m-%d-%Y') <= '04-02-2014';
And this will also work --
SELECT *
FROM `sells`
WHERE <your column name> between DATE_FORMAT('01-01-2013','%m-%d-%Y')
and DATE_FORMAT('04-02-2014','%m-%d-%Y');