I got this error on select statement in MySQL.
First one without error :
select '2018-09-01' as rdate,
model,
part_no,
sum(part_cost*count(*))
from table1
left join table2 on table1.part_no = table2.part_no
where issue > '2018-09-01'
and issue_date <= '2018-09-30'
group by model, table1.part_no
2nd cause
1022 - Can't write; duplicate key in table '' (it is blank)
select '2018-09-01' as rdate, model, part_no, sum(part_costcount()) from table1 left join table2 on table1.part_no = table2.part_no where issue_date<='2018-09-30' group by model, table1.part_no
table1 have index on model,part_no and table1 have index on part_no.
I just remove issue > '2018-09-01'.
Can anyone help?
Finally, I simplify my query, actually I also use ' upper (model) as model' then try with model only, and it works. I just wonder why the error message doesn't pinpoint the cause of error. Of cause the real cause of error is still a mystery.
Related
I want remove duplicate record & use this query:
DELETE FROM news e
GROUP BY e.itemId, e.tag_id
HAVING COUNT(e.itemId) > 1
AND COUNT(e.tag_id) > 1
but get this error:
[Err] 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 'e
How can I do this?
I'm not sure what you're trying to acheive here, please expand your explanation to add a little more info. From what i see you actually need to create a sub query as you can't use GROUP BY directly on the delete, try something like this:
delete from table
where columnA in (
select columnA
from (
select columnA
from YourTable
group by columnA
having count(*) > 1
) t
)
Not exactly tailored to your problem but you should get the idea.
for remove duplicate records, use this query
DELETE
n1
FROM
news n1,
news n2
WHERE
n1.id < n2.id
AND n1.itemId = n2.itemId
AND n1.tag_id = n2.tag_id
AND n1.tag_id IS NOT NULL
I have an SQL table, "data", with the following columns:
id (int),
date1 (datetime),
date2 (datetime),
difference (float)
What I would like to do is update this table so that the "difference" column contains the difference in years (as a float) between date2 and date1, only when date2 doesn't equal '0000-00-00 00:00:00'
I was thinking of something along the lines
Update data m
SET difference = datediff("hh",m.date2, m0.date1)/8765 --there are 8765 hours per year so this should give me my float
FROM data m0
WHERE m.id = m0.id
AND m.date2 <> '0000-00-00 00:00:00';
Yet when I try this I get an error stating "SQL syntax error. Check your MySQL server version for the right syntax to use near data SET difference = datediff("hh,m.date2,m0.date10/8765) from 'd' at line 1"
How would I modify my sql statement to get the desired results?
EDIT:
I am using xampp's phpMyAdmin interface to run this sql statement
Update statements are suppose to be on the form:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
WHERE conditions;
Your from clause is thus irrelevant.
You can use joins and sub select-queries though to link to other tables.
-- There are excellent demos on how to achieve that (actually, a complete duplicate):
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
I figured it out, thanks to Norbert's advice
I used
update data m, data m0
set m.difference = datediff(date(m.date2),date(m0.date2))/365
where m.id = m0.id
and m.date2 <> '0000-00-00 00:00:00';
I was using the incorrect syntax for mysql datediff, which takes 2 dates as a parameter and returns the difference in days.
Try this syntax for update with multiple tables:
Update data m, data m0
SET m.difference = datediff("hh",m.date2, m0.date1)/8765
WHERE m.id = m0.id
AND m.date2 <> '0000-00-00 00:00:00';
Note that in the SET statement you also need to indicate which table you are updating else it will still give you a nice warning.
I have written this query
set sql_safe_updates=0;
delete from t1 where id < (select avg(id) from t1);
set sql_safe_updates=1;
but it giving an error
Error Code: 1093. You can't specify target table 't1' for update in FROM clause
please resolve.
You can't specify the same table used in the delete in the rest of the query. One way to solve this is using a join:
delete t
from t1 t join
(select avg(id) as avgid
from t1
)
on id < (select avg(id);
This is a limitation in MySQL. Another way to get around it is to use an additional level of subqueries:
delete from t1 where id < (select avgid from (select avg(id) as avgid from t1));
The reason this works is because MySQL will materialize the subquery. The compiler sort of "forgets" that it is querying from the table bing modified, because of the extra level of subqueries.
I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql 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 sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1
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.