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`
...
Related
I recently update mysql to 8.0. Since this update a syntax error is reported in this query:
UPDATE
MinMaxAvg AS tar
INNER JOIN (
SELECT
SUBSTR(YYYYDD, 1, 8) AS timestamp,
ROUND(AVG(rainfall), 1) AS rain_total_avg,
ROUND(MIN(rainfall), 1) AS rain_total_min,
ROUND(MAX(rainfall), 1) AS rain_total_max
FROM
(
(
SELECT
MAX(rain_total) - MIN(rain_total) AS rainfall,
substr(timestamp, 1, 8) AS YYYYDD
FROM
weather
WHERE
timestamp > 0
GROUP BY
substr(timestamp, 1, 8)
) AS T1
)
GROUP BY
SUBSTR(YYYYDD, 1, 8)
) AS sor ON tar.timestamp = sor.timestamp
SET
tar.rain_total_avg = sor.rain_total_avg,
tar.rain_total_min = sor.rain_total_min,
tar.rain_total_max = sor.rain_total_max
Error: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 ') GROUP BY SUBSTR(YYYYDD,1, 8)) AS sor ON tar.timestamp = sor.timestamp SET tar.
I already checked the query with several online check tools. All reported the code as okay.
What is wrong with the query?
I already checked the query with several online check tools. All reported the code as okay.
I had to remove the parantheses around T1 block:
UPDATE
MinMaxAvg AS tar
INNER JOIN (
SELECT
SUBSTR(YYYYDD, 1, 8) AS timestamp,
ROUND(AVG(rainfall), 1) AS rain_total_avg,
ROUND(MIN(rainfall), 1) AS rain_total_min,
ROUND(MAX(rainfall), 1) AS rain_total_max
FROM
(
SELECT
MAX(rain_total) - MIN(rain_total) AS rainfall,
substr(timestamp, 1, 8) AS YYYYDD
FROM
weather
WHERE
timestamp > 0
GROUP BY
substr(timestamp, 1, 8)
) AS T1
GROUP BY
SUBSTR(YYYYDD, 1, 8)
) AS sor ON tar.timestamp = sor.timestamp
SET
tar.rain_total_avg = sor.rain_total_avg,
tar.rain_total_min = sor.rain_total_min,
tar.rain_total_max = sor.rain_total_max
I found out that the windows based sql client "HeidiSQL" might be used to find syntax errors in queries.
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.
Can you help me on this error?
Mon, 15 Dec 2014 18:22:16 +0000
Error encountered during the process
The reported error was List error:
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 ') AS FLOAT)) AS datetime) < GETDATE()) AND company = '27074BAY') AND gages.ga' at line 6.
Here is my code,
$sql_statement = "SELECT gages.*
FROM gages
WHERE gages.company = '$selectcomp'
AND GAGES.isactive != '0'
AND gages.gage_sn IN
(SELECT schedgi.gage_sn FROM schedgi WHERE (CAST(FLOOR(CAST(schedgi.sched_due_date) AS FLOAT)) AS datetime) < GETDATE() AND company = '$selectcomp')
AND gages.gage_sn NOT IN
( SELECT gages.gage_sn
FROM gages
WHERE gages.company = '$selectcomp'
AND gage_sn IN
(SELECT schedgi.gage_sn FROM schedgi WHERE (CAST(FLOOR(CAST(schedgi.sched_due_date) AS FLOAT)) AS datetime) < GETDATE() AND company = '$selectcomp'))"
Choose different names for your aliases. FLOAT is a reserved word in MySQL: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.
You can wrap the reserved words in backticks, but in my opinion it isn't worth the hassle.
SELECT something AS `FLOAT`...
I have to use a condition in the time diff of mysql. I have to modify the following query:-
select
bugs_team_user_view.bug_id,
bugs_team_user_view.creation_ts
from
bugs_team_user_view, bugs_activity
where
bugs_team_user_view.team like "Red%"
and
timediff(
(select bugs_activity.bug_when from bugs_activity where bugs_activity.added = "RESOLVED"),
bugs_team_user_view.creation_ts) > "00:30:00"
and
bugs_team_user_view.bug_id=bugs_activity.bug_id
and bugs_team_user_view.creation_ts < "2013-08-17 00:00:00"
and bugs_team_user_view.creation_ts >"2013-08-01 00:00:00";
Getting error:-
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to usenear 'bugs_team_user_view.creation_ts < "2013-08-17 00:00:00" and bugs_team_user_view.' at line 1
Please help
Change it to
timediff(
(select bugs_activity.bug_when from bugs_activity where bugs_activity.added = "RESOLVED"),
bugs_team_user_view.creation_ts) > TIME('00:30:00')
it should work.
What could be wrong with this line:
'$query = "SELECT * FROM messages WHERE (rlat => '".$latmin."' AND rlat <= '".$latmax."') AND (rlon >= '".$lonmin."' AND rlon <= '".$lonmax."')";'
Error: Error in query: SELECT * FROM messages WHERE (r_lat =>
'55.4655951769' AND r_lat <= '55.496987423') AND (r_lon >=
'25.5338700398' AND r_lon <= '25.5989507602'). 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 '=> '55.4655951769'
AND r_lat <= '55.496987423') AND (r_lon >= '25.5338700398' AN' at line
1...
Thanks!
=> isn't a known operator. If you want the greater than or equal comparison operator, then you are after >=.
Incidentally, you might find that your query can be written more concisely with the BETWEEN ... AND ... operator:
SELECT *
FROM messages
WHERE rlat BETWEEN $latmin AND $latmax
AND rlon BETWEEN $lonmin AND $lonmax
You should also investigate passing variables into your SQL by way of parameters to prepared statements.