I am trying to query the database using the below query. It works fine when I run the query in mysql server. But when I run the same query in scala it throws error.
I want to get a single row in db whose cid_status is "unreserved" and cid_curprocess is 'process1'and update the same cid_status to "reserved" and get back the cid_issueid of that row
Below is the query.
val query = "SET #LastUpdateID := 0; UPDATE table_details INNER JOIN
(SELECT cid_issueid FROM cen_issue_details WHERE
cid_curprocess='process1' AND cid_status='unreserved' LIMIT 1) AS
final ON cen_issue_details.cid_issueid=final.cid_issueid SET
cen_issue_details.cid_status ='reserved',cen_issue_details.cid_issueid
= (SELECT #LastUpdateID := cen_issue_details.cid_issueid);SELECT #LastUpdateID AS LastUpdateID;
val rs = statement.executeUpdate(query)
I get the below error:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'UPDATE table_details INNER JOIN (SELECT cid_issueid FROM cen_issue_details W' at line 1
Also I am not sure whether to use executeUpdate or executeQuery since the query does both SELECT and UPDATE.
Statement.executeupdate(String) accepts only a single statement, but you have provided two.
See the docs at https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)
This question discusses the options for doing multiple statements in a single roundtrip with JDBC: Two different prepared statements in one single batch
Related
I have a pretty difficult query to ask so I want to use the WITH ... AS syntax to break it down to smaller steps. I am trying to build a dummy query to ses how it works, which basically selects the stations that belong to a certain company and just selects everything again.
WITH company_stations AS (
SELECT * FROM Station WHERE Station.stationProvider = 'olympia_odos'
) SELECT * FROM company_stations;
the inside query works fine on it's own
SELECT * FROM Station WHERE Station.stationProvider = 'olympia_odos'
But when I use it Inside the with as statement it gives me the very helpful error message
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 use near 'company_stations AS (SELECT stationName FROM Station WHERE Station.stationProvid' at line 1
I tried searching for the ERROR 1064 (42000) but it seems that everyone that faced this error was in the process of building, populating or accessing a DB. I have done all these things, but the specific query seems to be problematic.
Also I tried all the usual suspects such as ' or " or `, ( or no ( etc.
CTE expressions is not supported before MySQL 8.0
You can use sub-queries instead:
SELECT * FROM (
SELECT * FROM Station WHERE Station.stationProvider = 'olympia_odos'
) AS company_stations;
Trying to delete a specific amount of rows in a MySQL query, I am able to SELECT whatever I want to delete with the following command, getting the results I need:
select * from ns_cos ns where ns.created_at <>
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
However, when I try to delete the selected data with:
delete from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
I get:
SQL Error [1064] [42000]: (conn=5159) 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 'ns where ns.created_at not exists (select max(nsa.created_at) from ns_cos nsa wh' at line 1
What am I doing wrong?
Your immediate issue is that not all MySQL versions support aliasing the table directly in delete from. Furthermore, though, you cannot re-open the table you delete from in the from clause.
Consider using the delete ... join syntax.
delete ns
from ns_cos ns
inner join (
select month_year, max(nsa.created_at) created_at
from ns_cos nsa
group by month_year
) ns1 on ns1.month_year = ns.month_year and ns1.created_at <> ns.created_at
EXISTS in there not posible use IN clause, but you need to enclose the table in a seprate select, so that mysql thinks it is another table
delete from ns_cos ns
where ns.created_at not IN (select max(nsa.created_at) from (SELECT * FROM ns_cos) nsa where nsa.month_year = ns.month_year)
This happens when you join tables from two schemas, no matter whether you take aliases for the table names or not.
Error
SQL 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 use near '...'
delete !ALIAS! from table ALIAS ...
Strangely, only with aliases, you can get around this error in a one-step SQL. Tested in MySQL 5.7.
Try this pattern, using the alias of the table you want to delete from between delete and from:
delete t1 from table t1
join table2 t2
on t1.id = t2.id
Your code would be:
delete ns from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
Other steps I checked before finding out the alias trick (do not read)
I tried it with a view of the other schema's table in the same schema, that is not enough.
One way to get around this is to make a copy of the table in the same schema (and delete that copy afterwards).
You might also somehow make a full "linked server link" like in T-SQL [linkedservername].DB1.Schema.Table1 at How to join two tables if they are in different schemas Your code is on the same server, but this might still help jumping over to the other schema, untested.
I have the following query :
update event
set event.Paid = payment.amount
from event, payment
where payment.event_id = event.eid
The above query gives following 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 'from event, payment where payment.event_id = event.eid' at line
3
You join clause is wrong anyway
You should not use the implicit join syntax based on comma separated table name and where
you should use explicit join syntax (for mysql)
update event
INNER JOIN payment ON payment.event_id = event.eid
set event.Paid = payment.amount
I'm trying to delete from two different tables but I've got an error when tried to run the SQL command.
I'm trying to combine this two queries:
defaultAdapter.query('DELETE FROM reasons WHERE name = :name')
defaultAdapter.query('DELETE rt FROM reason_to_transaction rt INNER JOIN reasons r ON r.id = rt.reason_id WHERE name = :name')
I run this code:
defaultAdapter.query('DELETE FROM reasons WHERE name = :name;DELETE rt, r FROM reason_to_transaction rt INNER JOIN reasons r ON r.id = rt.reason_id WHERE id = :id',
Could someone tell me what is the right syntax for 2 queries in Sequelize method(SQL).
When I run this command I've got this error:
Unhandled rejection SequelizeDatabaseError: 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 'DELETE rt, r FROM reason_to_transaction rt INNER JOIN reasons r ON r.id = rt.rea' at line 1
at Query.formatError
It does not allow you to combine two queries into one call.
If you need them tied together in a single "transaction" (all or none), then you need some form of START TRANSACTION and COMMIT.
I'm learning sql from these wikibooks pages and I'm trying to answer the last question "Remove all boxes from saturated warehouses" using mysql syntax. The following is what I came up with on my own mainly using previous answers from the same page.
delete from Boxes as b
where b.Warehouse in (
select w.Code
from ( select *
from Warehouses) as w
where w.Capacity < ( select count(*)
from Boxes bb inner join Warehouses ww
on bb.Warehouse = ww.Code
group by bb.Contents) ) ;
The query for this question on the site is generating
this solution doesn't work with mysql 5.0.67
ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause
That's why I'm using multiple sub queries as a way to come around this mysql message. However, I have an error in my query and therefore it's not running.
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 'as b where b.Warehouse in ( select w.Code
from ( sel' at line 1
Any idea?
I assume Boxes and Warehouse are tables, you gave Boxes an alias and used it on the Warehouse table, that's probably why it didn't work. Maybe you should try with a query after 'delete from'.
I think your warehouses table returns from than one columns (since * is used). Replace * with the column u want to get the output from.