Get previous day inserted data from table using MYSQL - mysql

I want to get data of previous day which inserted in table
SELECT *
FROM tbl_stockpricemaster AS sm
WHERE DATE(sm.inserted_on)=DATE(NOW()- INTERVAL 1 DAY)
AND sm.stock_keyvalue='positive'
GROUP BY sm.stock_id
ORDER BY sm.stock_difprice DESC
LIMIT 5;
This is my query but the problem is that when there is no data inserted on previous day then it show blank but i want to show the last inserted record

You will have to find out which date is the previous date
This can be done using this statement:
SELECT MAX(inserted_on) FROM tbl_stockpricemaster WHERE inserted_on < DATE(NOW())
So, changing your query to this should solve your problem:
SELECT *
FROM tbl_stockpricemaster AS sm
WHERE DATE(sm.inserted_on)=(SELECT MAX(inserted_on)
FROM tbl_stockpricemaster
WHERE inserted_on < DATE(NOW()) )
AND sm.stock_keyvalue='positive'
GROUP BY sm.stock_id
ORDER BY sm.stock_difprice DESC
LIMIT 5;

Related

Getting last 30 days of records

I have a table called 'Articles' in that table I have 2 columns that will be essential in creating the query I want to create. The first column is the dateStamp column which is a datetime type column. The second column is the Counter column which is an int(255) column. The Counter column technically holds the views for that particular field.
I am trying to create a query that will generate the last 30 days of records. It will then order the records based on most viewed. This query will only pick up 10 records. The current query I have is this:
SELECT *
FROM Articles
WHERE DATEDIFF(day, dateStamp, getdate()) BETWEEN 0 and 30
LIMIT 10
) TOP10
ORDER BY Counter DESC
This query is not displaying any records, but I don't understand what I am doing wrong. Any suggestions?
The MySQL version of the query would look like this:
SELECT a.*
FROM Articles a
WHERE a.dateStamp >= CURDATE() - interval 30 day
ORDER BY a.counter DESC
LIMIT 10;
Your query is generating an error. You should look at that error before fixing the query.
The query would look different in SQL Server.

Select last row in MySQL statment

here is the mysql statement :
SELECT
MAX(`history_card`.`PART_NUMBER`) AS `PART_NUMBER`, `history_card`.`SERIAL_NUMBER`,
MAX(`history_card`.`POSITION`) AS `POSITION`,
MAX(`history_card`.`RELEASE_DATE_TO_AIRCRAFT`) AS `RELEASE_DATE_TO_AIRCRAFT`,
MAX(`history_card`.`DATE_OFF_AIRCRAFT`) AS `DATE_OFF_AIRCRAFT`,
MAX(`history_card`.`LAST_CAP_CHECKED_DATE`) AS `LAST_CAP_CHECKED_DATE`,
MAX(`history_card`.`DUE_CAP_CHECK_DATE`) AS `DUE_CAP_CHECK_DATE`,
MAX(`history_card`.`JOB_REMARKS`) AS `JOB_REMARKS`,
MAX(`history_card`.`TSO`) AS `TSO`,
MAX(`history_card`.`BO_NUMBER`) AS `BO_NUMBER`,
MAX(`history_card`.`REPAIR_ORDER_NUMBER`) AS `REPAIR_ORDER_NUMBER`,
MAX(`history_card`.`LAST_OVERHAULED_DATE`) AS `LAST_OVERHAULED_DATE`,
MAX(`history_card`.`DUE_OVERHAUL_DATE`) AS `DUE_OVERHAUL_DATE`,
MAX(`history_card`.`REFRESHER_DATE`) AS `REFRESHER_DATE`,
MAX(`history_card`.`REFRESHER_DONE`) AS `REFRESHER_DONE`,
MAX(`history_card`.`GRN_ISSUE_DATE`) AS `GRN_ISSUE_DATE`,
MAX(`history_card`.`WORKSHEET`) AS `WORKSHEET`,
MAX(`history_card`.`ADDITIONAL_ATTACHMENT`) AS `ADDITIONAL_ATTACHMENT`,
MAX(`history_card`.`GRN`) AS `GRN`
FROM `history_card`
GROUP BY `history_card`.`SERIAL_NUMBER`
HAVING (((MAX(`history_card`.`DUE_CAP_CHECK_DATE`)) BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL -60 DAY) ))
ORDER BY MAX(`history_card`.`DUE_CAP_CHECK_DATE`) DESC;
Instead of MAX() I need to select the last row , meaning that the ID DESC has to be selected and limited to 1 record : ORDER BY id DESC LIMIT 1 but right now It is returning the max value , Coz I'm converting this query from MS , MS used LAST() function to get the last record by in mysql I need a similar solution for MySQL
If I understood the problem correctly, something on these lines should work:
SELECT
`history_card`.`SERIAL_NUMBER`,
`history_card`.`PART_NUMBER` AS `PART_NUMBER`,
`history_card`.`POSITION` AS `POSITION`,
`history_card`.`RELEASE_DATE_TO_AIRCRAFT` AS `RELEASE_DATE_TO_AIRCRAFT`,
`history_card`.`DATE_OFF_AIRCRAFT` AS `DATE_OFF_AIRCRAFT`,
`history_card`.`LAST_CAP_CHECKED_DATE` AS `LAST_CAP_CHECKED_DATE`,
`history_card`.`DUE_CAP_CHECK_DATE` AS `DUE_CAP_CHECK_DATE`,
`history_card`.`JOB_REMARKS` AS `JOB_REMARKS`,
`history_card`.`TSO` AS `TSO`,
`history_card`.`BO_NUMBER` AS `BO_NUMBER`,
`history_card`.`REPAIR_ORDER_NUMBER` AS `REPAIR_ORDER_NUMBER`,
`history_card`.`LAST_OVERHAULED_DATE` AS `LAST_OVERHAULED_DATE`,
`history_card`.`DUE_OVERHAUL_DATE` AS `DUE_OVERHAUL_DATE`,
`history_card`.`REFRESHER_DATE` AS `REFRESHER_DATE`,
`history_card`.`REFRESHER_DONE` AS `REFRESHER_DONE`,
`history_card`.`GRN_ISSUE_DATE` AS `GRN_ISSUE_DATE`,
`history_card`.`WORKSHEET` AS `WORKSHEET`,
`history_card`.`ADDITIONAL_ATTACHMENT` AS `ADDITIONAL_ATTACHMENT`,
`history_card`.`GRN` AS `GRN`
FROM `history_card`
JOIN
(
SELECT MAX(`history_card`.`ID`) ID, `history_card`.`SERIAL_NUMBER`
FROM `history_card`,
WHERE `history_card`.`DUE_CAP_CHECK_DATE` BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL -60 DAY)
GROUP BY `history_card`.`SERIAL_NUMBER`
) HC
ON `HC`.`ID`=`history_card`.`ID`
AND `HC`.`SERIAL_NUMBER`=`history_card`.`SERIAL_NUMBER`
SELECT TOP 1 * FROM .... ORDER BY ID DESC
should get you your last record.

pulling records from mysql where the difference between 2 dates is greater than 8 days

According to this documentation from my understanding using INTERVAL 8 DAY will return any records greater than 8 days.
In my statement $moztimestampnow is the current date in this format 2015-05-21 and moztimestamp pertains to the column in the DB that contains the other earlier date in which I need to calculate with.
I am not sure if I am able to use moztimestamp as the column name in this statement and it is not working.
How do I get the difference in days?
$moztimestampnow = date('Y-m-d');
SELECT *,DATEDIFF('$moztimestampnow',moztimestamp) INTERVAL 8 DAYS FROM backlinks WHERE user_id = '$user_id' LIMIT 10
First, you a misinterpreting the documentation. The interval keyword is for adding values to dates. If you want to filter data, you need to use the where clause.
In your case, the best where clause looks like this:
SELECT bl.*, DATEDIFF('$moztimestampnow', moztimestamp)
FROM backlinks bl
WHERE user_id = '$user_id' and
moztimestamp <= DATE_SUB(CURDATE(), INTERVAL 8 DAY)
LIMIT 10
This can take advantage of an index on backlinks(user_id, moztimestamp). In addition, you probably should have an ORDER BY clause. That is expected when using LIMIT.
Your syntax doesn't make sense. Try something like:
SELECT *
FROM backlinks
WHERE DATE_SUB(moztimestamp, INTERVAL 8 DAY) > '$moztimestampnow'
AND user_id = '$user_id'
LIMIT 10
I don't follow your objective, so you may have to change the order and direction in the first WHERE clause.

Dynamic sql query for broken hours data

Consider a table named "Books" every one hour new entry will be created in UTC format. I want to fetch last 24 hours data (24 entries) in my local timezone.
I tried this
select * from books where created_at >= DATE_SUB(NOW(),INTERVAL 24 HOUR);
How do I fetch records at runtime for the past 24 hours without setting timezone in mysql? Also the created data comes under three different id's, i need the data belonging to id=1.
(EDITED) Try this :
select * from books
where DAYOFMONTH(DATE(created_at))<>DAYOFMONTH(NOW())
order by DATE(created_at) desc
limit 24;
Now If you want to change the Time zone, for a session use this query :
SET GLOBAL TIME_ZONE = 'ASIA/CALCUTTA'
But the only disadvantage is you have to write it every time you restart MySQL.
To change it permananently, Put the following in your mysql server configuration (e.g. my.cnf):
default-time-zone=ASIA/CALCUTTA
And the only last last option left is conversion in query itself :
select convert_tz(created_at,'UTC', 'ASIA/CALCUTTA ') MyTimeZone from books
where DAYOFMONTH(DATE(created_at))<>DAYOFMONTH(NOW())
order by MyTimeZone desc
limit 24;
Try this:
SELECT * FROM books WHERE created_at > DATE_SUB(NOW(),INTERVAL 24 HOUR);
I wanted to comment on Sagar Joon answer but I cannot so I am adding separate one:
select * from books
where DATE(created_at) < date(now())
order by DATE(created_at) desc
limit 24;

My join sql query won't bring results

What could be wrong with my sql query here , I'd like to retrieve data from both tables meeting a WHERE condition
SELECT *, UNIX_TIMESTAMP(i.sent_date) AS udate
FROM ibc_sent_history as i INNER JOIN
ibc_messages as u
ON i.msg_ids = u.id
WHERE (i.sent_date >= '02-02-2013' AND i.sent_date <= '02-02-2014')
ORDER BY i.sent_date
LIMIT 200
Assuming your ibc_sent_history.sent_date datatype is DATETIME, here's a way to refactor this query. (This will work even if the datatype is DATE). You need to change your date input string format from 02-02-2013 to the more standard '2014-02-02` (YYYY-MM-DD).
SELECT whatever, whatever
FROM ibc_sent_history AS i
INNER JOIN ibc_messages AS u ON i.msg_ids = u.id
WHERE i.sent_date >= '2013-02-02'
AND i.sent_date < '2014-02-02' + INTERVAL 1 DAY
ORDER BY i.sent_date DESC
LIMIT 200
I changed the ORDER BY to include DESC. This is to return the most recent items, not the oldest. If that's not what you need, take off the DESC.
I changed the date formatting.
I changed the end of your selection range to
i.sent_date < '2014-02-02` + INTERVAL 1 DAY
That's because
i.sent_date <= '2014-02-02`
will include items that occur precisely at midnight on 2-Feb-2014, but won't include any other items on that day. What you probably want are items that occurred up to but NOT including midnight on the next day.
I don't know MySQL very well, but in SQL Fiddle when I run:
CAST('2014-02-02' AS DATE)
I get a date, when I run
CAST('02-02-2014' AS DATE)
I get NULL, so seems like your date format is wrong.
Demo: SQL Fiddle