Select date for today - php

Table: id, confess, user_ip, time, url, loves, hate
Time is like 1413040760
$q = mysql_query("SELECT * FROM confessions where time >= unix_timestamp(curdate() + interval 1 day)") or die(mysql_error());
I need the best confess of day order by loves limit 1. This show my only blank, no results.

You're querying records that happened later than one day from now - i.e., in the future. Presumably, you don't have any records like that. You can change the + interval 1 day to - interval 1 day in order to get records that occurred up to 1 day ago.
$q = mysql_query("SELECT * FROM confessions where time >= unix_timestamp(curdate() - interval 1 day)") or die(mysql_error());
EDIT:
To answer the question in the comment, yes, it's possible to sort by loves - hates - just slap on an order by clause:
$q = mysql_query("SELECT * " .
"FROM confessions " .
"WHERE time >= unix_timestamp(curdate() - interval 1 day) " .
"ORDER BY (loves - hates) DESC" ) or die(mysql_error());

Related

PHP SELECT to query date field plus 6 hours

I would like to query a date field to select all entries that are equal to or greater than current time + 6 hours. The commented first entry works, but it is the second entry that I am battling with. I know this syntax is nowhere near correct.
//$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND date_added >= CURRENT_DATE ORDER BY order_id DESC";
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND CURRENT_TIME >= date_added +6 hours";
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND NOW()>= DATE_ADD(date_added,INTERVAL 6 HOUR)";
This query will get your date_added value and add 6 hours to it and after will compare it with the CURRENT_TIME. I would suggest to use NOW() instead of CURRENT_TIME in mysql.
You're probably looking for the DATE_ADD (documentation) function. In your case:
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND your_date_field >= DATE_ADD(CURRENT_TIME, INTERVAL 6 HOURS)";
would likely do what you need it to.
try this query
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND CURRENT_TIME >= DATE_ADD(date_added, INTERVAL 6 HOUR)";

Calculate the difference b/w two months data

I have a mysql table orders in that I have a column order_date which is current time stamp(2016-08-17 00:00:00.000000). now I want to select or count the data's entered this month and the previous month, after this I can find the difference between these two months I am using this code and it is not working.
$sql="SELECT * FROM order WHERE order_date > DATE_SUB(NOW(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
this is not working an mysql error is produced.
Try
$sql="SELECT * FROM order WHERE DATE(order_date) LIKE DATE_SUB(CURDATE(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
i think this Link[http://sqlhints.com/2015/07/10/how-to-get-difference-between-two-dates-in-years-months-and-days-in-sql-server/] will help you
Use this. Hope it helps what you want. Thanks
$todayDate = date('Y-m-d');
$todayMonth = date("m", strtotime($todayDate ));
$previousMonth = $todayMonth - 1;
$sql = "SELECT * FROM order WHERE MONTH(order_date) BETWEEN '$todayMonth' AND '$previousMonth'";
First, the following is the correct logic to get all values from the current month and all of the previous month:
select *
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Then, use conditional aggregation for comparison. Here is an easy way:
select sum(month(order_date) = month(curdate())) as cur_month,
sum(month(order_date) <> month(curdate())) as prev_month,
(sum(month(order_date) = month(curdate())) -
sum(month(order_date) <> month(curdate()))
) as diff
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Note: I don't fully see the utility of comparing a partial month (this month) to a full month (last month), but that is what you seem to be asking for. If you are asking for something different, then ask another question with sample data and desired results.

Select everything older than 48 hours

I am trying to display user comments from a database that are older than 2 days. This is so I can screen the comments before they appear on the site. The date is entered into the database using CURDATE()
I have tried multiple ways to get this to work, but no joy so far, here's my line of code that tries to compare the dates.
$comments = "SELECT comments, initial, surname, theDate " .
"FROM tbl " .
"WHERE comments IS NOT NULL AND " .
"theDate < (unix_timestamp(CURDATE() - interval 2 day)) " .
"ORDER BY theDate DESC";
Everything gets displayed using this method.
Try to use DATEDIFF() MySQL function:
SELECT
comments, initial, surname, theDate
FROM
tbl
WHERE
comments IS NOT NULL
AND
DATEDIFF(NOW(), theDate) > 2
ORDER BY theDate DESC
use:
NOT BETWEEN CURDATE() - INTERVAL 2 DAY AND CURDATE()
and that should get you everything that is older than 2 days

mysql query get today and tommrows date

SELECT
doctors. fullname,
dutyroster.date,
dutyroster.time
FROM
dutyroster
INNER JOIN doctors ON doctors.docid = dutyroster.docid
WHERE doctors.docid = $doc_id AND
dutyroster.date = DATE(NOW()) AND DATE(NOW())+ INTERVAL 1 DAY
ORDER BY dutyroster.`date` ASC";
this query is used to find specific doctors information from a table called dutyroster. i want to get the docs shedule information for current day and tommrow only.. but this doesnt work.
and i made a second one which is also not working since it returns current one and all the next dates also
SELECT
doctors. fullname,
dutyroster.date,
dutyroster.time
FROM
dutyroster
INNER JOIN doctors ON doctors.docid = dutyroster.docid
WHERE doctors.docid = $doc_id AND
DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= dutyroster.date
ORDER BY dutyroster.`date` ASC"
Instead of
... AND dutyroster.date = DATE(NOW()) AND DATE(NOW())+ INTERVAL 1 DAY
try
... AND (dutyroster.date = CURDATE() OR
dutyroster.date = CURDATE() + INTERVAL 1 DAY))
or in more concise way, as #MarcM suggested
... AND dutyroster.date IN (CURDATE(), CURDATE() + INTERVAL 1 day)
From your first attempt it almost looks like you are trying to program COBOL!
Also, for future reference "this doesn't work" is not a helpful comment. You should say what actually happens.
Anyway, try changing your where clause to either:
WHERE doctors.docid = $doc_id AND
(dutyroster.date = CURRENT_DATE OR dutyroster.date = CURRENT_DATE + INTERVAL 1 DAY)
or:
WHERE doctors.docid = $doc_id AND
dutyroster.date IN (CURRENT_DATE, CURRENT_DATE + INTERVAL 1 DAY))

PHP / MySQL - Construct a SQL query

Im having a little trouble constructing a query.
I have a table with 3 columns.
id - day - pageviews
What i basically want to do is get 8 id's from the table where the pageviews are the highest from the last 60 days.
The day column is a datetime mysql type.
Any help would be great, im having a little trouble figuring this one out.
Cheers,
Almost the same as TuteC posted, but you'll need a group by to get what you need...
SELECT id, SUM(pageviews) totalViews
FROM table
WHERE DATE_SUB(CURDATE(), INTERVAL 60 DAY) <= day
GROUP BY id
ORDER BY totalViews DESC
LIMIT 8
Do something like this:
SELECT id FROM table_name
WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day
ORDER BY pageviews DESC
LIMIT 8;
$sixtyDaysAgo = date('Y-m-d',strtotime('-60 days'));
$sql = "SELECT id
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
ORDER BY pageviews DESC
LIMIT 8";
If each row is a number of pageviews for that day, and you're looking for the highest total sum of 60 days' worth, then you'll need to total them all and then grab the top 8 from among those totals, like so:
$sql = "SELECT id
FROM (
SELECT id, SUM(pageviews) AS total_pageviews
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
GROUP BY id
) AS subselect
ORDER BY total_pageviews DESC
LIMIT 8";

Categories