I have a MySQL table with the following schema:
CREATE TABLE `events` (
`id` bigint(20) NOT NULL,
`name` text NOT NULL,
`time` text NOT NULL,
`city` text NOT NULL,
`description` text NOT NULL,
`contact_name` text NOT NULL,
`contact_number` text NOT NULL,
`contact_email` text NOT NULL,
`image` text NOT NULL,
`main_event` int(11) NOT NULL DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now we can see that time column is type of text. A sample entry from time is given below:
12/09/2017 05:45:00 pm
12/09/2017 02:00:00 pm
13/09/2017 10:30:00 am
Now what I want is to get entries separated by Day i.e. entries for 12/09/2017 and 13/09/2017, also they must be sorted by time in ascending order.
So my sample output will be like below
12/09/2017 02:00:00 pm
12/09/2017 05:45:00 pm
then
13/09/2017 10:30:00 pm
I tried to get ID using group concat and tried to convert this text type date string using the combination of DATE_FORMAT and STR_TO_DATE.
But the problem is that I am not able to sort it correctly. If I use %p argument with STR_TO_DATE the output becomes NULL.
I am currently using the following query
SELECT GROUP_CONCAT(id) id, DAY(DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y'), '%Y-%m-%d')) day, MONTHNAME(DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y'), '%Y-%m-%d')) month, DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y'), '%Y-%m-%d') test FROM events WHERE main_event >= 0 GROUP BY DAY(DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y'), '%Y-%m-%d')), MONTH(DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y'), '%Y-%m-%d')) ORDER BY test ASC
And the output is:
id day month test
13,10,14 11 September 2017-09-11
15,16,19 12 September 2017-09-12
But what I want is that id should also be sorted based on hour, i.e. 9:00:00 am entry will come first, then 10:00:00 am entry.
Also on a second thought, I tried to sort them after getting the result like below:
SELECT DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y %h:%i %p'), '%d/%m/%Y %h:%i %p') t FROM events WHERE id = 15 or id = 16 or id = 19 ORDER BY t ASC
But the result is NULL
I found my error myself. Actually, I missed parsing seconds(%s) given in the time value.
DATE_FORMAT(STR_TO_DATE(time, '%d/%m/%Y %h:%i:%s %p'), '%H:%i') t
Related
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 have this table structure
CREATE TABLE `fixtures` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date` date NOT NULL DEFAULT '2017-03-23',
`goalsHomeTeam` int(11) NOT NULL DEFAULT '0',
`goalsAwayTeam` int(11) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
)
I want to get the items where date is either today or greater but whenever day matches it should only bring the items for that date only. So if I have 4 items in 23 Feb, and 3 items in 25 Feb, being both are greater than today I want only items for 23 Feb!
I have tried
SELECT *
FROM fixtures
WHERE date >= NOW();
But this brings both 23rd's and 25th's items!
To get the nearest date greater than today's date you can use the following query:
select min(date) from fixtures where date>current_date
or this:
select date from fixtures
where date>current_date
order by date limit 1
(current_date returns the current date without time information, so you can just use > instead of >=). Then your query will be like this:
select * from fixtures
where date=(select min(date) from fixtures
where date>current_date)
SELECT f.*
FROM fixtures f
INNER JOIN
(SELECT MIN(date) min_date
FROM fixtures
WHERE date >= NOW()) filter
ON filter.min_date = f.date
Try this:
SELECT *
FROM fixtures
WHERE date >= NOW() AND date = (SELECT MIN(date) FROM fixtures);
This will always return the elements from the smallest possible date you have stored in your database, which will always be today or greater than today.
I'm in need of your help.
What I'm trying to achieve is the following:
Obtain both the withdrawal and deposit profit, for each day, for the past week.
So I'm hoping to get rows with the values: Day, Deposit Profit, Withdrawal Profit.
The catch however is that a day is a custom day, meaning: A day is between yyyy-mm-dd 13:00:00 and yyyy-mm-dd 13:00:00. So a group by date wouldn't be sufficient.
The query I've tried experimenting with was:
SELECT submit_date,
MAX(deposit_amount) - MIN(deposit_amount) AS deposit,
SUM(withdrawal_amount * withdrawal_percentage) as withdrawal
FROM `pro_Profits`
WHERE account_id = '{C795E1D2-452A-DEE8-A800-02E94332114A}'
AND submit_datetime >= NOW() - INTERVAL 1 WEEK
GROUP BY submit_date
ORDER BY `submit_datetime` DESC
Table:
CREATE TABLE IF NOT EXISTS `pro_Profits` (
`id` varchar(512) NOT NULL,
`account_id` varchar(512) NOT NULL,
`submit_date` date NOT NULL,
`submit_time` time NOT NULL,
`submit_datetime` datetime NOT NULL,
`deposit_amount` bigint(20) NOT NULL,
`withdrawal_amount` bigint(20) NOT NULL,
`deposit_percentage` double NOT NULL DEFAULT '1',
`withdrawal_percentage` double NOT NULL DEFAULT '0.4',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`),
KEY `account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
What you basically need to do is shift the day by 13 hours. You can use a function for this in MySQL:
TIMESTAMPDIFF(HOUR,13,submit_date)
In your SQL query this would look something like this:
SELECT
TIMESTAMPDIFF(HOUR,13,submit_date) as shifted_submit_date,
MAX(deposit_amount)-MIN(deposit_amount) AS deposit,
SUM(withdrawal_amount*withdrawal_percentage) as withdrawal
FROM
pro_Profits
WHERE
account_id = '{C795E1D2-452A-DEE8-A800-02E94332114A}' AND
submit_datetime >= NOW()-INTERVAL 1 WEEK
GROUP BY
shifted_submit_date
ORDER BY
submit_datetime DESC
A bit of experimenting might be needed to get exactly what you want. I find it strange that you group by one thing, and order by another.
You can try something like this:
SELECT
FLOOR(TIME_TO_SEC(TIMEDIFF(DATE_ADD(Date(NOW()), INTERVAL 13 Hour),submit_datetime))/86400.00) as Diff,
MAX(deposit_amount)-MIN(deposit_amount) AS deposit,
SUM(withdrawal_amount*withdrawal_percentage) as withdrawal
FROM
pro_Profits
WHERE account_id='{C795E1D2-452A-DEE8-A800-02E94332114A}'
and submit_datetime >= DATE_ADD(Date(NOW()), INTERVAL 13 Hour)-INTERVAL 1 WEEK
GROUP BY
Diff
ORDER BY
Diff
DATE_ADD(Date(NOW()), INTERVAL 13 Hour: You want to start from today at 13:00 and go back 1 week
TIME_TO_SEC(TIMEDIFF(DATE_ADD(Date(NOW()), INTERVAL 13 Hour),submit_datetime))/86400.00: Calculate difference in seconds between our date and 'submit_datetime'
FLOOR(...): we get the upper bound of that difference to create our day "buckets".
Note: count of "buckets" is actually 8, you can also find "-1" if there is a submit on the day you cast your query after 13:00. You can easily edit the above query to remove those results.
I have the following table:
CREATE TABLE `visitors_table` (
`ID` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`visitor_ip` VARCHAR(32) NULL,
`visitor_browser` VARCHAR(255) NULL,
`visitor_hour` SMALLINT(2) NOT NULL DEFAULT '00',
`visitor_minute` SMALLINT(2) NOT NULL DEFAULT '00',
`visitor_date` DATE NOT NULL,
`visitor_day` SMALLINT(2) NOT NULL,
`visitor_month` SMALLINT(2) NOT NULL,
`visitor_year` SMALLINT(4) NOT NULL,
`visitor_page` VARCHAR(255) NULL
);
The following query gets the visitors per day:
"SELECT visitor_date FROM visitors_table WHERE visitor_date = CURDATE()";
Now I want output the data hourly. See this example:
Day Year Hours Clicks
July 4, 2011 00:00:00 4
July 4, 2011 01:00:00 12
July 4, 2011 02:00:00 75
July 4, 2011 03:00:00 27
and so on...
Could someone help me? I can't get clear with the output of the total visitors by hours for one day!
With the following query comes only this result:
SELECT visitor_date, count(*) FROM visitors_table WHERE visitor_date = CURDATE() GROUP BY visitor_hour
visitor_date count(*)
2013-08-31 4
2013-08-31 7
I need to get the results by hours like written as above!
Where is my mistake or how can i show it with php?
SELECT visitor_date, count(*) FROM visitors_table WHERE visitor_date = CURDATE() group by visitor_hour