所有表我試圖產生類似於此的曲線圖:運行MySQL查詢通過R中
從與多個表的數據庫(如x軸)。
數據:
我在數據庫中具有如下幾個表:Table1, Table2, Table3, etc
每個表有500+行和列10+(屬性)
的問題
一個這些列中包含該消息的條件(公平,良好,非常好等)。
在數據庫返回此查詢:
SELECT message_condition Condition,
COUNT(message_id) NumMessage
FROM `table1` GROUP message_condition
這將返回:
------------------------
Condition | NumMessage
------------------------
| 80
Fair | 20
Good | 60
Ideal | 50
Great | 80
更新:總是有4個條件有一個空條件(消息沒有條件)。所以,如果我運行所有表的查詢,我將得到與上面相同的表,但具有不同的數字。
現在,我想將這些查詢應用於數據庫中的所有表,所以我可以生成上面的圖(以表格爲x軸)。
我試圖用這個方法:
doCountQuerys <- function(con, table) {
query <- paste('
SELECT message_condition Condition,
COUNT(message_id) NumMessage FROM`', table, '` GROUP message_condition', sep = '')
ts <- dbGetQuery(con, query)
return(ts)
}
lists <- dbListTables(con) # get list of all tables in the database
countz <- numeric(0) # store the counts for all tables
for (i in 1:length(list)) {
counta <- doCountQuerys(con, lists[i])
countz[i] <- counta[[1]]
#print(countz[[1]])
}
,但我得到這個錯誤:
## Warning in countz[i] <- counta[[1]]: number of items to replace is not a
## multiple of replacement length
我不認爲我正確地做這個,任何想法如何通過運行該查詢R中的所有表並生成該圖?
您的查詢是否會爲數據庫中的每個表生成相同數量的消息條件和計數? –
@TimBiegeleisen否,每個表的每個條件的消息計數是不同的。例如,對於table1,公平條件的NumMessage爲table2時爲20,公平條件的NumMessage爲80.並且始終有4個條件爲null。 –