SELECT database data With Date - php

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?

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';

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';

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%';

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

Related

SQL SUM() All with check from other table

Table con_projects
mysql> SELECT * FROM con_projects;
+----+---------------+---------------------+--------+
| id | project_name | project_description | status |
+----+---------------+---------------------+--------+
| 1 | Project 1 | Description IS Here | 1 |
| 2 | Project 2 | Description IS Here | 0 |
| 3 | Project 3 | Description IS Here | 1 |
| 4 | Project 4 | Description IS Here | 0 |
| 5 | Project 5 | Description IS Here | 1 |
+----+---------------+---------------------+--------+
Here status 1=active and 0=inactive
mysql> SELECT * FROM con_transactions;
+------+------+---------------------+--------+
| t_id | p_id | date | amount |
+------+------+---------------------+--------+
| 10 | 1 | 2016-02-17 19:24:05 | 1800 |
| 12 | 2 | 2016-02-18 11:40:13 | 200 |
| 17 | 3 | 2016-02-18 11:42:04 | 100 |
| 19 | 4 | 2016-02-18 11:45:43 | 1 |
| 20 | 5 | 2016-02-18 11:45:54 | 999 |
| 21 | 1 | 2016-02-18 11:46:02 | 1500 |
| 41 | 2 | 2016-02-18 17:23:14 | 500 |
| 42 | 3 | 2016-02-18 17:23:14 | 500 |
| 43 | 4 | 2016-02-18 17:23:15 | 500 |
| 44 | 5 | 2016-02-18 17:23:16 | 500 |
| 45 | 1 | 2016-02-18 17:23:16 | 500 |
| 46 | 2 | 2016-02-18 17:23:16 | 500 |
| 47 | 3 | 2016-02-18 17:23:17 | 500 |
| 48 | 4 | 2016-02-18 17:23:17 | 500 |
| 49 | 5 | 2016-02-18 17:23:18 | 500 |
| 50 | 1 | 2016-02-18 17:25:54 | 1000 |
| 51 | 1 | 2016-02-18 17:26:22 | 3000 |
| 52 | 2 | 2016-02-18 17:48:59 | 10 |
| 53 | 1 | 2016-02-18 17:48:59 | 10 |
| 55 | 1 | 2016-02-19 10:20:12 | 1000 |
+------+------+---------------------+--------+
Now I want to SUM() all amount from con_trnsactions whose p_id from con_projects status = 1
I tried this and many more
mysql> SELECT (SELECT SUM(t.amount) FROM con_transactions t WHERE p.id=t.p_id) as total FROM con_projects p WHERE status='1'
+-------+
| total |
+-------+
| 14120 |
+-------+
AND
mysql> SELECT SUM(amount) from con_transactions;
+-------------+
| SUM(amount) |
+-------------+
| 14120 |
+-------------+
Both Are Same . But The subtraction will be like this.
mysql> SELECT 1800+100+999+1500+500+500+500+500+500+1000+3000+10+1000;
+---------------------------------------------------------+
| 1800+100+999+1500+500+500+500+500+500+1000+3000+10+1000 |
+---------------------------------------------------------+
| 11909 |
+---------------------------------------------------------+
here is the all active project (con_trsaction.p_id=con_projects.id AND con_projects.status=1) id (p_id) From con_trsanction.amount
Try this:
SELECT SUM(t.amount)
FROM con_transactions t
INNER JOIN con_projects AS p ON t.p_id = p.id
WHERE p.status = 1
It's a simple join operation. Only 'active' projects will be accounted for in the summation.
SELECT SUM(amount) from con_transactions LEFT JOIN con_projects ON con_projects.id = con_transactions.p_id WHERE con_projects.status = 1;

SELECT monthly record form database

mysql> SELECT * FROM con_transactions;
+------+------+---------------------+--------+
| t_id | p_id | date | amount |
+------+------+---------------------+--------+
| 10 | 1 | 2016-02-17 19:24:05 | 1800 |
| 12 | 2 | 2016-02-18 11:40:13 | 200 |
| 17 | 3 | 2016-02-18 11:42:04 | 100 |
| 19 | 4 | 2016-02-18 11:45:43 | 1 |
| 20 | 5 | 2016-02-18 11:45:54 | 999 |
| 21 | 1 | 2016-02-18 11:46:02 | 1500 |
| 41 | 2 | 2016-02-18 17:23:14 | 500 |
| 42 | 3 | 2016-02-18 17:23:14 | 500 |
| 43 | 4 | 2016-02-18 17:23:15 | 500 |
| 44 | 5 | 2016-02-18 17:23:16 | 500 |
| 45 | 1 | 2016-02-18 17:23:16 | 500 |
| 46 | 2 | 2016-02-18 17:23:16 | 500 |
| 47 | 3 | 2016-02-18 17:23:17 | 500 |
| 48 | 4 | 2016-02-18 17:23:17 | 500 |
| 49 | 5 | 2016-02-18 17:23:18 | 500 |
| 50 | 1 | 2016-02-18 17:25:54 | 1000 |
| 51 | 1 | 2016-02-18 17:26:22 | 3000 |
| 52 | 2 | 2016-02-18 17:48:59 | 10 |
| 53 | 1 | 2016-02-18 17:48:59 | 10 |
| 55 | 1 | 2016-02-19 10:20:12 | 1000 |
+------+------+---------------------+--------+
HOW Can I Select current month or last month All Transactions record. All Month are not 30 days. So How can I Select January or other month record with SELECT query. I Try this query
SELECT * FROM con_transactions WHERE date > '2016-01-01 00:00:00' AND date < '2016-02-01 00:00:00';
This is not so smart .
Use month() and year() to directly find any given month of a year.
SELECT * FROM con_transactions WHERE year(date) = 2016 AND month(date) = 1;
One method is to use formats such as as YYYYMM for the date comparison:
SELECT *
FROM con_transactions
WHERE YEAR(date) * 100 + MONTH(date) = YEAR(CURDATE())*100 + MONTH(CURDATE());
However, that has the drawback that MySQL cannot use an index for the WHERE clause. So, another method is a variant of your method but using the first day of the month:
WHERE date >= str_to_date(concat_ws('-', year(curdate()), month(curdate()), '01'), '%Y-%m-%d) and
date < str_to_date(concat_ws('-', year(dateadd(curdate(), interval 1 month)), month(dateadd(curdate(), interval 1 month), '01'), '%Y-%m-%d)

Mysql query for calculating power outage between two time stamps

This is a revised version of my question.
I am currently designing a simple power quality monitoring tool. I have managed to design a MySQL database and populate the table with the voltage status and the corresponding time stamp taken at particular times. Below is my table structure.
+----+---------------------+-------------+
| Id | Time_Stamp | Red_Ph_Volt |
+----+---------------------+-------------+
| 1 | 2015-02-01 17:33:45 | 250.00 |
| 2 | 2015-02-01 18:53:41 | 250.00 |
| 3 | 2015-02-01 18:54:39 | 25.00 |
| 4 | 2015-02-01 18:54:54 | 242.00 |
| 5 | 2015-02-01 18:55:11 | 222.00 |
| 6 | 2015-02-02 21:00:29 | 250.00 |
| 7 | 2015-02-02 21:00:45 | 220.00 |
| 8 | 2015-02-02 21:00:55 | 230.00 |
| 9 | 2015-02-02 21:03:01 | 230.00 |
| 10 | 2015-02-02 21:03:36 | 250.00 |
| 11 | 2015-02-02 21:03:46 | 50.00 |
| 12 | 2015-02-06 17:54:08 | 0.00 |
| 13 | 2015-02-06 23:04:04 | 220.00 |
| 14 | 2015-02-06 23:04:34 | 220.00 |
| 15 | 2015-02-06 23:05:51 | 250.00 |
| 16 | 2015-02-08 16:04:44 | 220.00 |
| 17 | 2015-02-08 16:06:29 | 220.00 |
| 18 | 2015-02-09 09:04:12 | 220.00 |
| 19 | 2015-02-09 10:42:39 | 203.00 |
| 20 | 2015-02-09 19:34:43 | 203.00 |
| 21 | 2015-02-09 21:57:02 | 203.00 |
| 22 | 2015-02-10 09:47:08 | 0.00 |
| 23 | 2015-02-10 11:15:34 | 250.00 |
| 24 | 2015-02-10 11:48:14 | 250.00 |
| 25 | 2015-02-10 13:18:14 | 220.00 |
| 26 | 2015-02-10 18:59:52 | 0.00 |
| 27 | 2015-02-10 22:44:14 | 250.00 |
| 28 | 2015-02-10 22:47:10 | 212.00 |
| 29 | 2015-02-14 00:02:10 | 212.00 |
| 30 | 2015-02-14 00:28:57 | 242.00 |
| 31 | 2015-02-14 00:35:56 | 21.00 |
| 32 | 2015-02-16 12:11:47 | 21.00 |
+----+---------------------+-------------+
32 rows in set (0.02 sec)
Any voltage below 50V is taken as an outage. I want to write a query that queries the table for the TOTAL OUTAGE TIME between 2015-02-01 17:33:45 and 2015-02-14 00:28:57.
Guys, kindly help me on this because I really don’t know how to approach it.
This should work:
SELECT sum(timestampdiff(SECOND, Time_Stamp, (SELECT Time_Stamp
FROM voltage AS V2
WHERE V2.Id > V1.Id
LIMIT 1))) AS T
FROM voltage AS V1
WHERE Red_Ph_Volt <= 50 AND Time_Stamp BETWEEN '2015-02-01 17:33:45' AND '2015-02-14 00:28:57';
I have assumed that a voltage of 50 is also considered as an outage and I have assigned the table name as voltage.
The first step to properly address this kind of queries is to join every row with its previous row.
SELECT *
FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)
Now you just have to keep those rows which its previous measure was below 50 and sum their timestamp differences.
SELECT SUM(TIMESTAMPDIFF(SECOND, PRV_V.Time_Stamp, V.Time_Stamp))
FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)
WHERE PRV_V.Red_Ph_Volt < 50
AND V.Time_Stamp BETWEEN '2015-02-01 17:33:45' AND '2015-02-14 00:28:57'
EDIT: If your data don't have autoincremental ids or you have gaps between your ids you can still join your measurements with its previous measurement using the timestamp:
SELECT *
FROM voltage AS V
LEFT JOIN voltage AS PRV_V
ON ( PRV_V.Time_Stamp < V.Time_Stamp
AND NOT EXISTS ( SELECT *
FROM voltage
WHERE Time_Stamp > PRV_V.Time_Stamp
AND voltage.Time_Stamp < V.Time_Stamp
)
)

SQL request for several tables and operations in MySQL

I have the following 3 tables: unit, stage, stats.
unit stage
+----+--------+ +----+-------+---------------------+
| id | status | | id |unit_id| date |
+----+--------+ +----+-------+---------------------+
| 1 | 2 | | 1 | 2 | 2013-11-22 00:00:00 |
| 2 | 3 | | 2 | 2 | 2013-11-26 12:00:00 |
| 3 | 3 | | 3 | 3 | 2013-10-11 00:00:00 |
| 4 | 0 | | 4 | 1 | 2013-12-29 00:00:00 |
+----+--------+ +----+-------+---------------------+
stats
+----+----------+---------------------+-------+
| id | stage_id | date | clicks|
+----+----------+---------------------+-------+
| 1 | 1 | 2013-11-22 00:00:00 | 10 |
| 2 | 1 | 2013-11-23 00:00:00 | 20 |
| 3 | 1 | 2013-11-24 00:00:00 | 25 |
| 4 | 2 | 2013-11-26 00:00:00 | 15 |
| 5 | 2 | 2013-11-27 12:00:00 | 21 |
| 6 | 3 | 2013-12-29 00:00:00 | 8 |
+----+----------+---------------------+-------+
I need a request, that will produce the following response:
+---------+---------------------+-----------------------+
| unit.id | stage.min.date | sum(stats.max.clicks) |
+---------+---------------------+-----------------------+
| 2 | 2013-11-22 00:00:00 | 46 |
| 3 | 2013-12-29 00:00:00 | 8 |
+---------+---------------------+-----------------------+
by the following rules:
1) unit.id - show only units with unit.status=3
2) stage.min.date - minimal stage.date for corresponding unit_id
3) sum(stats.max.clicks) - sum of stats.clicks with max dvalues for each stage_id associated with corresponding unit_id. In my example 46 = 25(stage_id=1) + 21(stage_id=2)
The problem is in min.date and sum of clicks - I have no idea how to get it in one query. Definitely it`s not a problem to do it using php code and several requests.
Schema in SQL Fiddle
Thanks in advance.
I just ask myself, why I do this? Your example resonse has an error, and does not match your fiddle... but:
SELECT
cc.unit_id, MIN(cc.date) as stage_min_date , SUM(dd.clicks) as stats_max_clicks
FROM
stage cc
LEFT JOIN (
SELECT
bb.stage_id, bb.clicks
FROM
stats bb LEFT JOIN (
SELECT id, stage_id, MAX(date) AS max_date
FROM stats
GROUP BY stage_id
) aa
ON
aa.max_date = bb.date
WHERE
aa.max_date IS NOT NULL
) dd
ON cc.id = dd.stage_id
LEFT JOIN unit ee
ON ee.id = cc.unit_id
WHERE ee.status = 3
GROUP BY cc.unit_id
...

Selecting rows with max value in range

I have the followng data:
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 1 | 1 | 2013-11-01 23:59:59 | 12 |
| 2 | 1 | 2013-11-02 23:59:59 | 15 |
| 3 | 1 | 2013-11-03 23:59:59 | 18 |
| 4 | 1 | 2013-11-04 23:59:59 | 29 |
| 5 | 1 | 2013-11-05 23:59:59 | 38 |
| 6 | 1 | 2013-11-05 12:59:59 | 40 |
| 7 | 1 | 2013-11-06 23:59:59 | 45 |
| 8 | 1 | 2013-11-07 23:59:59 | 49 |
| 9 | 1 | 2013-11-08 23:59:59 | 52 |
| 10 | 2 | 2013-11-04 23:59:59 | 25 |
| 11 | 2 | 2013-11-05 21:59:59 | 42 |
| 12 | 2 | 2013-11-06 23:59:59 | 60 |
| 13 | 2 | 2013-11-07 23:59:59 | 75 |
| 14 | 2 | 2013-11-08 23:59:59 | 86 |
| 15 | 2 | 2013-11-09 23:59:59 | 90 |
| 16 | 2 | 2013-11-10 23:59:59 | 92 |
| 17 | 2 | 2013-11-11 23:42:59 | 98 |
+----+---------+---------------------+-------+
I would pass a day and wish to get the rows with the highest time in the given day or if there`s no records for this site for this day, the last available row in the past.
e.g. for 2013-11-01
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 1 | 1 | 2013-11-01 23:59:59 | 12 |
+----+---------+---------------------+-------+
for 2013-11-05
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 5 | 1 | 2013-11-05 23:59:59 | 38 |
| 11 | 2 | 2013-11-05 21:59:59 | 42 |
+----+---------+---------------------+-------+
and for 2013-11-10
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 9 | 1 | 2013-11-08 23:59:59 | 52 |
| 16 | 2 | 2013-11-10 23:59:59 | 92 |
+----+---------+---------------------+-------+
Thanks in advance.
You can try this:
SELECT a.id,a.site_id,b.maxDate,a.views
FROM table1 a
INNER JOIN (
SELECT site_id ,MAX(datetime) as maxDate
FROM table1
WHERE datetime < DATEYOUWANTTOSEE + INTERVAL 1 DAY
GROUP BY site_id
) b ON a.site_id = b.site_id AND a.datetime = b.maxDate
The inner query will get you the MAX(datetime) for each site_id. Then you join it with your table to get the rest of the information.
sqlfiddle demo
SELECT * FROM <tablename> WHERE datetime = <datetime> ORDER BY datetime DESC LIMIT 2
You data and examples do not really match themselves, nor the description, but what you probably are looking for is this:
Select top 1 *
from table
where date(datetime) <= date(#PARAMETER)
order by datetime desc
I believe this should work:
SELECT SUBSTRING(`datetime`, 1, 10) AS date, MAX(`views`)
FROM table
GROUP BY SUBSTRING(`datetime`, 1, 10)
If you need the id/site_id as well, write it in your post, as it is not clear
SQLFiddle to show you the result:
http://sqlfiddle.com/#!2/e0ccb3/1

Categories