I have a MySQL database with multiple tables and those tables contain multiple columns that are equal, and I want to push that data into a google spreadsheet.
For example:
table1
contains id
, name
, page
, and a couple of columns specific for that table.
table2
contains id
, name
, page
, and a couple of different columns
table3
also has id
, name
, page
, and more unique columns, etc.
This is my code:
function readFromTable() {
var ss = SpreadsheetApp.getActive();
var sheetDetails = ss.getSheetByName('Details');
var sheetData = ss.getSheetByName('Clients');
var host = sheetDetails.getRange("B1").getValue();
var databaseName = sheetDetails.getRange("B2").getValue();
var userName = sheetDetails.getRange("B3").getValue();
var password = sheetDetails.getRange("B4").getValue();
var port = sheetDetails.getRange("B5").getValue();
var url = 'jdbc:mysql://'+host+':'+port+'/'+databaseName;
Logger.log(url);
Logger.log(userName);
Logger.log(password);
var sql = 'SELECT id, name, page FROM table1 UNION SELECT id, name, page FROM table2 UNION SELECT id, name, page FROM table3';
try{
var connection = Jdbc.getConnection(url, userName, password);
var results = connection.createStatement().executeQuery(sql);
var metaData = results.getMetaData();
var columns = metaData.getColumnCount();
// Retrieve metaData to a 2D array
var values = [];
var value = [];
var element = '';
// Get table headers
for(i = 1; i <= columns; i ++){
element = metaData.getColumnLabel(i);
value.push(element);
}
values.push(value);
// Get table data row by row
while(results.next()){
value = [];
for(i = 1; i <= columns; i ++){
element = results.getString(i);
value.push(element);
}
values.push(value);
}
// Cloese connection
results.close();
// Write data to sheet Data
SpreadsheetApp.flush();
sheetData.getRange(2, 1, values.length, value.length).setValues(values);
SpreadsheetApp.getActive().toast('The data has been updated.');
}catch(err){
SpreadsheetApp.getActive().toast(err.message);
}
}
Is it possible to select id, name and page from all three tables in one query and get the result in arrival order? I entered the same id in each table one by one, I mean, 1
in table1
, 1
in table2
and 1
in table3
in that order, then I did the same with 2
and etc, this is how the output should look:
id
name
page
1
name1
toys
1
name2
food
1
name3
electronics
2
name4
toys
2
name5
food
2
name6
electronics
3
name7
toys
3
name8
food
3
name9
electronics
But in the output they get grouped by table, and that doesn't work me:
id
name
page
1
name1
toys
2
name4
toys
3
name7
toys
1
name2
food
2
name5
food
3
name8
food
1
name3
electronics
2
name6
electronics
3
name9
electronics
I would post a picture, but I do not have enough reputation.
I need the output in arrival order, any suggestions?
您可以使用ORDER BY
子句:
var sql = '(SELECT id, name, page FROM table1) UNION (SELECT id, name, page FROM table2) UNION (SELECT id, name, page FROM table3) ORDER BY id';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.