I am trying to Bulk insert records from a table but the column names from the table need to be inserted into columns in my MySQL db with names which are different.
The logic is below:
INSERT INTO Tbl_samples (Sample_ID, Date_Sampled, Easting_m, Northing_m, Elevation_m)
SELECT
item.Wpt_SampleID AS Sample_ID,
item.Date_taken AS Date_Sampled,
item.Easting AS Easting_m,
item.Northing AS Northing_m,
item.Elevation_m AS Elevation_m,
FROM
(SELECT * FROM {{table1.data}}) AS item
WHERE
item.Wpt_SampleID IS NOT NULL
AND item.Wpt_SampleID LIKE '3%';
However I am getting the error
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 ''[object Object]', '[object Object]', '[object Object]', '[object Object]', '...' at line 10
INSERT INTO Tbl_samples (Sample_ID, Date_Sampled, Easting_m, Northing_m, Elevation_m)
SELECT
item.Wpt_SampleID AS Sample_ID,
item.Date_taken AS Date_Sampled,
item.Easting AS Easting_m,
item.Northing AS Northing_m,
item.Elevation_m AS Elevation_m
FROM
(SELECT * FROM {{formatDataAsArray(table1.data)}}) AS item
WHERE
item.Wpt_SampleID IS NOT NULL AND
item.Wpt_SampleID LIKE '3%';
Now getting this error 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 ') AS item WHERE item.Wpt_SampleID IS NOT NULL AND item.Wpt_SampleID ...' at line 9
I think you need to take the ( ) out of the SELECT clause on line 9:
SELECT * FROM {{formatDataAsArray(table1.data)}} AS item
At least, that is what the error looks like it is saying. The other thing to try would be maybe to put the closing ) at the end of the whole second SELECT clause -- after the WHERE.
Hi there! One note is that table.data is probably already an array, so it won't necessarily help to wrap it in formatDataAsArray(). You may still run into syntax issues beyond the table data and the parentheses
I would recommend this approach instead:
Create a Query JSON with SQL query that selects the table data and updates the column names
2. Then, run a sql update using our GUI mode & referencing this new query:
You could also solve this using a single SQL query with Javascript inside the "Array of records to insert" field:
I am also trying to format the date to a new format from the table. It is a string like this: 2023-07-11T02:28:44Z which I want to format to date 'yy-mm-dd'
And so I am trying to use the code below but getting the error message:"alasql.fn.STR_TO_DATE is not a function"
SELECT
item.Wpt_SampleID AS Sample_ID,
DATE_FORMAT(STR_TO_DATE(item.Date_taken, '%Y-%m-%dT%H:%i:%sZ'), '%Y-%m-%d') AS Date_Sampled,
item.Easting AS Easting_m,
item.Northing AS Northing_m,
item.Elevation_m AS Elevation_m
FROM
(SELECT * FROM {{ table1.data }}) AS item
WHERE
item.Wpt_SampleID IS NOT NULL AND
item.Wpt_SampleID LIKE '3%';
I tried using the code below also, which correctly formats my date, but I am trying to only include records which begin with 3, and getting the error: Duplicate entry '1' for key 'Sample_ID_Tbl_Sample'