所有表我试图产生类似于此的曲线图:运行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。 –