User variables - fighting syntax - mysql

Can anyone set my MySQL syntax straight please?
I am trying to set a user variable called "seconds" to equal the output of the query shown below (and it does work by it's self), but I am constantly geting "You have an error in your SQL syntax" errors.
SET #seconds=AVG(t2.epoch-t1.epoch)
FROM tmp_4045_metrics AS t1, tmp_4045_metrics AS t2
WHERE t1.seq+1 = t2.seq
I have tried SELECT AVG...., (SELECT AVG...), and quite a few others, but always the same error warning that my syntax is incorrect and never any clues to where it's incorrect.
Can anyone point me in the right direction?
Thanks,
Rich

If i remember correctly (and it works on MS SQL Server at work), this should do it:
Select #seconds=AVG(t2.epoch-t1.epoch)
FROM tmp_4045_metrics AS t1, tmp_4045_metrics AS t2
WHERE t1.seq+1 = t2.seq

Now the SELECT #seconds=... works error free - cracking :) and SELECT #seconds returns, but it's empty.
And I have triple checked the statement returns a value (300 seconds to be precise).
Back to the drawing board
Thanks for the suggestion though, better than I had achieved by myself.

In a SELECT statement, use the := assignment operator (not the = operator):
SELECT #seconds := AVG(t2.epoch-t1.epoch)
FROM tmp_4045_metrics AS t1, tmp_4045_metrics AS t2
WHERE t1.seq+1 = t2.seq

Related

how to correctly write an alias in a query SQL oracle

This is my query
CREATE VIEW CourseQueuePositions AS
SELECT t2.code , t2.cid ,
(SELECT COUNT(*) as queue
FROM Waits t1
WHERE t2.code = t1.code AND t1.queue# <= t2.queue#)
FROM Waits t2;
I keep getting this compile error
Error at Command Line : 3 Column : 9
00998. 00000 - "must name this expression with a column alias"
although the Oracle SQL developer doesn't indicate any error beforehand. Also I believe that I am using the alias "query" so I really don't understand. Help would be appreciated
Try putting the alias after the subquery instead of after the column name within the subquery.
Although, I'd rewrite the query thus:
CREATE VIEW CourseQueuePositions AS
SELECT t2.code , t2.cid , count(t1.code) queue
FROM Waits t1, Waits t2
WHERE t2.code = t1.code AND t1.queue# <= t2.queue# ;
I don't have access to Oracle now so I can't test this out. But give it a shot.
Upon reviewing your query, it's hard to tell exactly you're trying to do. Can you update the question with example data and an explanation of what you're trying to do?
I have come up with the solution. Previously I added the alias in the inner subqueries SELECT statement, however that wasn't the correct way. What I should have done is to make an alias of the ENTIRE subquery. So something like this...
CREATE VIEW CourseQueuePositions AS
SELECT t2.code, t2.cid,
(SELECT COUNT(*)
FROM Waits t1
WHERE t1.code = t2.code AND t1.queue# <= t2.queue#) AS queue
FROM Waits t2;

Complex MySQL cmd with JOINS and Counters doesnt work

I am working on this SQLFiddle and cant get the command working. Here the command:
SET #n := 1;
SET #start := '2013-07-22 10:00:01';
SET #end := '2013-07-22 10:00:02';
SET #register := 40001;
SELECT * FROM
(
SELECT
`realvalues`.`Timestamp`,
`realvalues`.`Value` * `register`.`Factor`,
#x := #x + 1 AS rank
FROM
`realvalues`,
(SELECT #x := 0) t
WHERE
`realvalues`.`Register` = register AND
`realvalues`.`Timestamp` BETWEEN start AND end
JOIN
`register`
ON
`register`.`DeviceID` = `realvalues`.`DeviceID` AND
`register`.`Register` = `realvalues`.`Register`
) a
WHERE
rank MOD ? = n
Does anybody know where the command fails? MySQL error reporting isnt very usefull.
[EDIT] The Value is now duplicated with Factor.
There are many many things wrong with your query. However, the error that is being reported in your fiddle is:
...check the manual that corresponds to your MySQL server version for the right syntax to use near 'BETWEEN start AND end JOIN register ON ...
Your syntax for BETWEEN is incorrect. There should be no IS token before BETWEEN. Correct syntax is:
<value> BETWEEN <lower-bound-inclusive> AND <upper-bound-inclusive>
Other problems include:
start, end, and n are not columns
register (in the WHERE clause) is ambiguous
You have a JOIN clause after a WHERE clause
You do not specify an alias for the second column of your derived table a (perhaps not necessary but may cause issues)
Use of a ? parameter without a way to specify a value (although this is a limitation of SQL Fiddle and not necessarily a problem with your SQL statement)
i dont see usage of #start and #end
EDIT: now it works !
sqlfiddle.com/#!2/6dc97/50

operation must use an updateable query - access

I want to update a table by using the join in access - 2007
UPDATE TABLE1 A INNER JOIN (SELECT ACCODE, SUM(AMOUNT) AS SUM_AMOUNT
FROM TABLE2 GROUP BY ACCODE) B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
it gives me error that
operation must use an updateable query
i have try with below query and no error is here
UPDATE TABLE1 A INNER JOIN TABLE2 B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
please help me to find what is wrong with first query
I think the reason Access treats your query as non-updateable is due to the subquery GROUP BY. You should be able to create an updateable query by using DSum.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE=" & a.ACCODE)
If ACCODE is text instead of numeric data type, add quotes around the value in the DSum expression.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE='" & a.ACCODE & "'")
I just had this issue and it was due to permissions on the table. I made a copy of the linked table to test my update query on, and kept getting this error message. I was pretty sure my query would be ok (it was a simple update) so I ran it on the actual table and I didn't get the error. Just wanted to add this for anyone else that comes across this in the future!
this error also comes when you are accessing database which is on different PC. so give write permission from security tab of folder and also give write access on sharing permission option.
In my case this is worked
Yes Mandeep, that is the way ms-access works.
UPDATE Table1 As T1 INNER JOIN Table2 AS T2 ON T1.F1 = T2.F1
SET T1.F2 = T2.F2
This query raises 'operation must use an updateable query' erro when T2 is a query, even when you are not updating T2.

MySQL query works in Navicat but generates MySQL Syntax Error elsewhere

Hi everyone and thanks for the help in advance. I'm frustrated because I'm writing relatively long sql queries (at least for a n00b like me) that work perfectly in NaviCat. I then import them into the dreamweaver sql editor (where I'm designing my site) and they come back with
> MySQL 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 '' at line 2
I'll post one of these queries and hopefully somebody can point out to me what I'm doing wrong.
SELECT (SELECT COUNT(GA_Potential.Potential_ID)
FROM GA_Potential
WHERE GA_Potential.Character_ID=14)+ (SELECT COUNT(GA_PurchasedPotential.PurchasedPotential_ID)From GA_PurchasedPotential Where GA_PurchasedPotential.Member_ID=2)+ (SELECT GA_Characters.StartPotential From GA_Characters WHERE GA_Characters.Character_ID =14)+ (SELECT FLOOR(iot_ActingPoints.Acting_Points/10) From iot_ActingPoints WHERE iot_ActingPoints.Member_ID=2)- (SELECT SUM(GA_AttributePoints.Determination_Points) FROM GA_AttributePoints WHERE GA_AttributePoints.Character_ID=14)- (SELECT SUM(GA_AttributePoints.Dexterity_Points) FROM GA_AttributePoints WHERE GA_AttributePoints.Character_ID=14)- (SELECT SUM(GA_AttributePoints.Productivity_Points) FROM GA_AttributePoints WHERE GA_AttributePoints.Character_ID=14)- (SELECT SUM(GA_AttributePoints.Sophistication_Points) FROM GA_AttributePoints WHERE GA_AttributePoints.Character_ID=14)- (SELECT SUM(GA_AttributePoints.Strength_Points) FROM GA_AttributePoints WHERE GA_AttributePoints.Character_ID=14)- (SELECT IFNULL(SUM(GA_CasteAbilitiesForList.Potential_Cost),0) FROM GA_CasteAbilitiesForList,GA_LearnedAblities WHERE GA_CasteAbilitiesForList.Ability_ID=GA_LearnedAblities.Ability_ID AND GA_LearnedAblities.Character_ID=14)- (SELECT IFNULL(SUM(GA_ProfAbilitiesForList.Potential_Cost),0) FROM GA_ProfAbilitiesForList,GA_LearnedAblities WHERE GA_ProfAbilitiesForList.Ability_ID=GA_LearnedAblities.Ability_ID AND GA_LearnedAblities.Character_ID=14) AS TOTAL
This generates exactly the number I want in NaviCat.
Edit: I stripped out all of the single quotes that NaviCat puts in automatically, and I still get the same error.
Thanks in advance.
-CB
Just a hip shot: maybe you need to use parentheses around all of the subselects, like
SELECT
((SELECT COUNT(`GA_Potential`.`Potential_ID`) From`GA_Potential` WHERE`GA_Potential`.`Character_ID`=14)+
(SELECT COUNT(`GA_PurchasedPotential`.`PurchasedPotential_ID`)From`GA_PurchasedPotential` Where`GA_PurchasedPotential`.`Member_ID`=2)+
(SELECT `GA_Characters`.`StartPotential` From `GA_Characters` WHERE `GA_Characters`.`Character_ID` =14)+
(SELECT FLOOR(`iot_ActingPoints`.`Acting_Points`/10) From `iot_ActingPoints` WHERE `iot_ActingPoints`.`Member_ID`=2)-
(SELECT SUM(`GA_AttributePoints`.`Determination_Points`) FROM `GA_AttributePoints` WHERE `GA_AttributePoints`.`Character_ID`=14)-
(SELECT SUM(`GA_AttributePoints`.`Dexterity_Points`) FROM `GA_AttributePoints` WHERE `GA_AttributePoints`.`Character_ID`=14)-
(SELECT SUM(`GA_AttributePoints`.`Productivity_Points`) FROM `GA_AttributePoints` WHERE `GA_AttributePoints`.`Character_ID`=14)-
(SELECT SUM(`GA_AttributePoints`.`Sophistication_Points`) FROM `GA_AttributePoints` WHERE `GA_AttributePoints`.`Character_ID`=14)-
(SELECT SUM(`GA_AttributePoints`.`Strength_Points`) FROM `GA_AttributePoints` WHERE `GA_AttributePoints`.`Character_ID`=14)-
(SELECT IFNULL(SUM(`GA_CasteAbilitiesForList`.`Potential_Cost`),0) FROM `GA_CasteAbilitiesForList`,`GA_LearnedAblities` WHERE `GA_CasteAbilitiesForList`.`Ability_ID`=`GA_LearnedAblities`.`Ability_ID` AND `GA_LearnedAblities`.`Character_ID`=14)-
(SELECT IFNULL(SUM(`GA_ProfAbilitiesForList`.`Potential_Cost`),0) FROM `GA_ProfAbilitiesForList`,`GA_LearnedAblities` WHERE `GA_ProfAbilitiesForList`.`Ability_ID`=`GA_LearnedAblities`.`Ability_ID` AND `GA_LearnedAblities`.`Character_ID`=14))
AS total
also: does the error message really end at the near?
Had the same problem - leaving this here for other who run into this:
Dreamweaver doesn't like multiple SELECT statements in the SQL. I was able to re-work my query to not need multiple SELECT statements and the error went away.
As an additional note, I thought it was the parentheticals, but they seem to work fine with other functions.

What does ER_WARN_FIELD_RESOLVED mean?

When SHOW WARNINGS after an EXPLAIN EXTENDED shows a
Note 1276 Field or reference 'test.foo.bar' of SELECT #2 was resolved in SELECT #1
What exactly does that mean and what impact does it have?
In my case, it prevents MySQL from using what seems to be a perfectly good index. But it's not about fixing that specific query (as it is an irrelevant test).
I found http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html butError: 1276 SQLSTATE: HY000 (ER_WARN_FIELD_RESOLVED)
Message: Field or reference '%s%s%s%s%s' of SELECT #%d was resolved in SELECT #%d
Isn't much of an explanation?
You may want to use EXPLAIN in JSON format, using:
EXPLAIN FORMAT=JSON SELECT ...
It gives you a better picture how MySQL interprets the query, the JSON structure is hierarchical. But the outcome highly depends on the query and table structure.
In my case the warning was in perfectly fine WHERE clause in EXISTS subquery. This over-simplified query still produces the warning:
EXPLAIN SELECT a.id
FROM a
WHERE a.id = 1
AND EXISTS(SELECT 1 FROM c WHERE c.id = a.id)
What I figured out is, EXPLAIN was having problem with the c.id = a.id, because I already established, that a.id = 1, so the proper EXISTS, according to my ancient MySQL 5.7.9, would be:
EXISTS(SELECT 1 FROM c WHERE c.id = 1)