Can't insert into temporary table in MySQL - mysql

I'm using NodeJS v8.7.0 with the promise-mysql module, alongside mysql v14.14 Distrib 5.5.57.
I um unable to insert into the temporary table I create. On online SQL validators, as well as my project, I receive the error around the INSERT command.
Whilst I can personally see no issues, I'd love to see a fresh perspective of things to spot the issue.
My whole query string:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;DROP TEMPORARY TABLE QueryBuilder;
Query line-by-line:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;
INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);
UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;
DROP TEMPORARY TABLE QueryBuilder;
Extra info:
'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 \'INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1,\' at line 1'
errno: 1064
An SQLFiddle demonstrating why I made this query
Thanks for reading my issue, I appreciate all feedback!

Related

I can't figure out syntax error in complex join statement mysql 8.0

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.

How to do an insert with blocks of selects?

What i'm doing is a simple insert using a select statement to retrieve data from other tables, and here is the problem, i want to do that insert but with multiple inserts trying to imitate a insert with block insertion in fact, if i run the code below it works but just for one row:
INSERT INTO factmonitoreo (idEstacion, IdVariable, IdTiempoRegistro, Valor, ValorOriginal, FlagRegistro)
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:29', 0, 0, 0
FROM dimestacion AS dt
INNER JOIN dimtipoestacion AS dte ON dt.IdTipoEstacion = dte.IdTipoEstacion
JOIN dimvariable AS dv
WHERE dt.Estacion LIKE 'El Lago'
AND dte.CodTipoEstacion LIKE 'ECT'
AND dv.ColumnNameToManualUpdateFromCSV LIKE 'Temperatura')
But if i append a second Select the query doesn't works, see below:
INSERT INTO factmonitoreo (idEstacion, IdVariable, IdTiempoRegistro, Valor, ValorOriginal, FlagRegistro)
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:29', 0, 0, 0
FROM dimestacion AS dt
INNER JOIN dimtipoestacion AS dte ON dt.IdTipoEstacion = dte.IdTipoEstacion
JOIN dimvariable AS dv
WHERE dt.Estacion LIKE 'El Lago'
AND dte.CodTipoEstacion LIKE 'ECT'
AND dv.ColumnNameToManualUpdateFromCSV LIKE 'Temperatura'),
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:28', 0, 0, 0
FROM dimestacion AS dt
INNER JOIN dimtipoestacion AS dte ON dt.IdTipoEstacion = dte.IdTipoEstacion
JOIN dimvariable AS dv
WHERE dt.Estacion LIKE 'El Lago'
AND dte.CodTipoEstacion LIKE 'ECT'
AND dv.ColumnNameToManualUpdateFromCSV LIKE 'Temperatura')
This piece of code raises the following error:
[Code: 1064, SQL State: 42000] 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 dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:28', 0, 0, 0
FROM ...' at line 8 [Script position: 1792 - 1853]
I have read this documentation to do an attempt of insert with multiple selects: 13.2.7.1 INSERT ... SELECT Statement
I don't think it support multiple selects in a single insert statement. just split it into 2 sql, that will be fine...
Look at the About This Manual session,
The ellipsis in SELECT ... context is more like the omission of the statement.
Sometimes ellipsis will indicate the preceding element can be repeated. but usually it will be presented as <element>, [<element>]...

Problem with syntax when try add trigger in mysql

I need help with my SYNTAX error
I try to add trigger into mysql by still have syntax error.
I will be glad for any help
When I try add this trigger i still have error 1064
Full error response is: 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 '' at line 19 MySQL version is 5.7
CREATE TRIGGER delete_przejazdy_logger AFTER DELETE ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 1, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
OLD.data, OLD.osob, OLD.fotelik, OLD.detale, OLD.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON OLD.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON OLD.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON OLD.id_adres_z = adresy_z.id
WHERE pasazerowie.id = OLD.id_pasazera);
END;
I add triggers on this same db for logs on creating records and works fine.
Here is the code which works good:
CREATE TRIGGER add_przejazdy_logger AFTER INSERT ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 2, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
NEW.data, NEW.osob, NEW.fotelik, NEW.detale, NEW.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON NEW.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON NEW.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON NEW.id_adres_z = adresy_z.id
WHERE pasazerowie.id = NEW.id_pasazera);
END;

Cannot insert data into database using pymysql, what is wrong with my syntax?

I am trying to insert some data into a table in mysql. But there is something wrong with my sql syntax, and I just couldn't find it, cannot find anything online either.
Tried using the raw string to insert as well, got the same error message.
def save_room_info(data):
conn = pymysql.connect(...)//this goes right
try:
with conn.cursor() as cursor:
table = 'room'
keys = ', '.join(data.keys())
values = ', '.join(['%s'] * len(data))
sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values)
try:
if cursor.execute(sql, tuple(data.values())):
print('Successful')
conn.commit()
except Exception:
print('Failed', sys.exc_info())
conn.rollback()
finally:
conn.close()
def main():
data = {
'title': 'a',
'screenshot': 'a',
'type': 0,
'viewCount': 0,
'nickname': 'Allen',
'level': 20,
'headUrl': 'a',
'tag': 'a',
'rank': 3,
'followerCount': 290
}
save_room_info(data)
There is an 'id' field in table 'room', which I set it to Auto Increment. And 'id' is the only primary key.
But even I add id during data insertion, it still gave the same error message.
Got this error message:
ProgrammingError(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 'rank, followerCount) VALUES ('a', 'a', 0, 0, 'Allen', 20, 'a', 'a', 3, 290)' at line 1"),
Rank is reserved keyword in mysql. Look here https://dev.mysql.com/doc/refman/8.0/en/keywords.html
You have two options
1. Rename column
2. Wrap column in backticks like rank

Unknown column in on clause, cannot reproduce in Workbench

The following query generates the error
Unknown column 'a.household_id' in 'on clause'
in phpmyadmin but not in MySQL Workbench. phpmyadmin (3.5.3) is looking at a remote server running MySQL server 5.5.27, Workbench 6.1.4 is looking at localhost running 5.5.37. [I have reproduced the unknown column error described in the MySQL documentation in Workbench so the docs provide no immediate direction for eliminating this error.]
SET #end_year = (select if(month(now()) < 7, year(now()) -1, year(now())));
SET #start_year = #end_year - 4;
SELECT if(MONTH(contact_date) >= 7, CONCAT('FY ', YEAR(contact_date) + 1), CONCAT('FY ', YEAR(contact_date))) AS FY,
FORMAT(SUM(A.size), 0) AS DI, FORMAT(COUNT(c.household_id), 0) AS DH
FROM contact c
JOIN
(
SELECT m.household_id, if(COUNT(dob) = 0, 1, COUNT(dob)) size
FROM member m
GROUP BY m.household_id
) A ON a.household_id = c.household_id
WHERE contact_date BETWEEN CONCAT(#start_YEAR - 1, '-07-01') AND CONCAT(#end_YEAR, '-06-30')
GROUP BY if(MONTH(contact_date) >= 7, YEAR(contact_date) + 1, YEAR(contact_date));
Your table alias is A not a and I guess it matters in your case. Try the following :
A ON A.household_id = c.household_id