Error Code : 1064 while trying to retrieve values between timestamps - mysql

In my table, I have entries (with datetime datatype) like:
and I need result between a specific UNIX time stamp. When I run the query:
Select ifnull(ji.JobID,'') as JobID, ifnull(ji.ReelIndex,'') as ReelIndex, ifnull(ji.FileName,'') as FileName,
ifnull(ji.MediaType,'') as MediaType, ifnull(ji.QCStatus,'') as QCStatus, ifnull(ji.QCComments,'') as QCComments,
ifnull(ji.PackagingStatus,'') as PackagingStatus, ci.Name
from job_info as ji
left join content_info as ci on
ji.ContainerID = ci.ID
where ji.Progress = 100
where ji.ProcessStartTime >= 1449081000
and ji.ProcessEndTime <= 1450549800
order by Name asc, ReelIndex asc
LIMIT 0 , 20
it throws an error:
Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right
syntax to use near 'where ji.ProcessStartTime >= 1449081000 and
ji.ProcessEndTime <= 1450549800 or' at line 4
What could be the reason for this?

You have double WHERE use AND/OR instead:
SELECT ifnull(ji.JobID,'') AS JobID,
ifnull(ji.ReelIndex,'') AS ReelIndex,
ifnull(ji.FileName,'') AS FileName,
ifnull(ji.MediaType,'') AS MediaType,
ifnull(ji.QCStatus,'') AS QCStatus,
ifnull(ji.QCComments,'') AS QCComments,
ifnull(ji.PackagingStatus,'') AS PackagingStatus,
ci.Name
FROM job_info AS ji
LEFT JOIN content_info AS ci
ON ji.ContainerID = ci.ID
WHERE ji.Progress = 100
AND ji.ProcessStartTime >= FROM_UNIXTIME(1449081000) -- here
AND ji.ProcessEndTime <= FROM_UNIXTIME(1450549800)
ORDER BY Name ASC, ReelIndex ASC
LIMIT 0, 20;
EDIT:
If you want to skip time part and set it to 00:00:00 CAST to DATE:
...
AND CAST(ji.ProcessStartTime AS DATE) >= FROM_UNIXTIME(1449081000)
AND CAST(ji.ProcessEndTime AS DATE) <= FROM_UNIXTIME(1450549800)
Note also that you need to use FROM_UNIXTIME to convert unix_timestamp to datetime.

Related

I want to add two new fields to a view which is not related to a table with a case statement

1) I am creating a Calendar View and I would like to add a column which checks if the date in that row is a working day if so then populate with 1, else 0.
I have a table with all UK public holidays and in the calendar table I also have a 'dayoftheweek' field where 1 and 7 is SUN and SAT.
Currently I am trying use a case statement, however I cannot run the script as receive the following error:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(CASE IF CT.dayofweek = `1` OR CT.dayofweek = `7` OR CT.dt = PH.`H' at line 12 (edited)
This is my script:
ALTER VIEW Calendar_View AS
select `calendar_table`.`dt` AS `date`,
`calendar_table`.`year` AS `year`,
`calendar_table`.`quarter` AS `quarter`,
`calendar_table`.`month` AS `month`,
`calendar_table`.`day` AS `day`,
`calendar_table`.`dayofweek` AS `dayofweek`,
`calendar_table`.`monthName` AS `monthName`,
`calendar_table`.`dayName` AS `dayName`,
`calendar_viewtable`.`week` AS `week`,
`calendar_table`.`isWeekday` AS `isWeekday`
(CASE
WHEN CT.dayofweek = `1` THEN `0'
WHEN CT.dayofweek = `7` THEN `0`
WHEN CT.dt = PH.`Holiday Date` THEN `0`
ELSE `1`
END CASE ) AS IsWorkingDay
from `calendar_table` CT, `Public_Holidays` PH
2) I would also like to add another field which checks if the date in the calendar is the current date and so the populate new field with Yes, else No (I tried using a case statement this didnt work).
This is your code:
(CASE WHEN CT.dayofweek = `1` THEN `0'
WHEN CT.dayofweek = `7` THEN `0`
WHEN CT.dt = PH.`Holiday Date` THEN `0`
ELSE `1`
END CASE ) AS IsWorkingDay
This is what you want:
(CASE WHEN CT.dayofweek = 1 THEN 0
WHEN CT.dayofweek = 7 THEN 0
WHEN CT.dt = PH.`Holiday Date` THEN 0
ELSE 1
END) AS IsWorkingDay
There are two important differences:
END CASE is not valid syntax.
When you use backticks, you are saying that the string is an identifier -- typically a column reference. There are no columns called 0 or 1.
Two other issues with your query are the missing comma and the lack of join conditions. Your from clause should look more like:
from calendar_table ct left join
Public_Holidays ph
on ct.dt = ph.dt -- or whatever the right condition is

SQL nested queries and subqueries

i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)
I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);

MYSQL syntax error, with date time

I am currently converting my api from SQL to MySQL, however one of my queries is failing at this point below.
The query:
SELECT u.DATETIME,
SUM(Value * m.Multiplier) * 1 AS Usage,
TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', UNIX_TIMESTAMP(u.DATETIME)) as Datestamp_epoch
FROM MeterMaps m
JOIN UsageElectricityDaily u ON m.ChanID = u.ChanID
JOIN SourceChannels c ON c.ChanID = m.ChanID
WHERE m.LocationID = 1
AND c.QuantityID = 'Usage'
AND u.DateStamp >= '01/12/2016 00:00:00'
AND u.DateStamp < '31/12/2016 00:00:00'
AND m.Utility = 'Electric'
GROUP BY u.Datestamp
The error that is thrown is
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'Usage, TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00',
UNIX_TIMESTAMP(u.DATETIME)) ' at line 1
In MySQL the word usage is a keyword, therefore it must be enclosed by backticks or change it.
SELECT u.DATETIME,
SUM(Value * m.Multiplier) * 1 AS `Usage`
...

Correct syntax to use in mysql 5.5.25 for not exists

Hi I'm trying to write a cursor in powerbuilder 12 to fetch some records . Here this is my query.
I'm trying to fetch some records from the trninvhdr table which are not in the second table.
SELECT
INV_DATE,
INV_NO,
INV_TYPE,
CUR_CODE,
EXCH_RATE,
usd_rate,
CR_TERM,
DUE_DATE,
bl_date,
TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE ='NFL1' AND
CUST_CODE = 'NLML' AND
INV_TYPE ='F' AND
INV_DATE <= '2016-03-25' AND
NOT EXISTS
(SELECT * FROM trninvoiceavailability WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
Here how I use it in the program.
DECLARE lc_retrieve CURSOR FOR
SELECT
trninvhdr.INV_DATE,
trninvhdr.INV_NO,
trninvhdr.INV_TYPE,
trninvhdr.CUR_CODE,
trninvhdr.EXCH_RATE,
trninvhdr.usd_rate,
trninvhdr.CR_TERM,
trninvhdr.DUE_DATE,
trninvhdr.bl_date,
trninvhdr.TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE = :as_comp_code AND
CUST_CODE = :as_cust_code AND
INV_TYPE ='F' AND
INV_DATE <= :as_inv_date )AND
NOT EXISTS (SELECT * FROM trninvoiceavailability
WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND
trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
open lc_retrieve ;
The query works fine in the mysql server but in the progra it gives me the following error .
Database c0038 SQLSTATE = 3700 MySQL ODBC 5.2 a Driver mysql id 5.5.25 You have an error in your syntax. check the manual that corresponds to your mysql version for the right syntax to use near NOT EXISTS (SELECT * FROM trninvoiceavailability.... at line 1.
What is the correct Syntax that I should use to work this query.
I can see a bracket in this code... where did it come from and where is it's friend?
INV_DATE <= :as_inv_date )AND
You need to remove ) from your query as per below-
INV_DATE <= :as_inv_date )AND
sholuld be INV_DATE <= :as_inv_date AND

SQL error #1064 when nesting a SELECT MAX in a LEFT JOIN

Please let me know why I get an error on the following SQL (I am using a MySQL database) and how to rectify it:
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
I broke the SQL down to ensure each part worked.
SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
And
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
Each of these work on their own. When I combine them I get the following error:
Error
SQL query:
LIMIT 0, 25
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 25' at line 1
Regards,
Glyn
EDIT
#Barmar came up with the solution to remove the ";" at the end when testing. However, the result was not what I expected so I ended up changing this to:
SELECT at_award_description.ad_id,
at_award_description.aw_id,
at_award_description.ad_group,
at_award_description.ad_detail,
at_award_description.ad_start_date,
at_award_description.ad_archived_date,
MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group