I am trying to create a Temp Table and populate data for a basic report
我正在尝试创建临时表并为基本报告填充数据
Create Statement
Create语句
Create temporary table attendance_report(first_name varchar(40), weeks_present integer, weeks_away integer, total_weeks integer)
I get a table with a column with no name at the start - So 5 in total instead of 4.
我得到了一个表,其中有一列在开头没有名称--所以总共是5个,而不是4个。
Then to populate I have the following:
然后,为了填充,我有以下几点:
INSERT INTO attendance_report (first_name, weeks_present, weeks_away, total_weeks)
Select first_name from members
Where id = 19
Union
Select Count(*) from Rolls join Rollmappings
on Rolls.roll_id = rollmappings.id
Where rolls.member_id = 19
and rollmappings.roll_year = '2023'
and status != 'A'
Union
Select Count(*) from Rolls join Rollmappings
on Rolls.roll_id = rollmappings.id
Where rolls.member_id = 19
and rollmappings.roll_year = '2023'
and status = 'A'
Union
Select Count(*) from Rolls join Rollmappings
on Rolls.roll_id = rollmappings.id
Where rolls.member_id = 19
and rollmappings.roll_year = '2023'
I receive the following:
Error Code: 1136. Column count doesn't match value count at row 1
我收到以下信息:错误代码:1136。列计数与第1行的值计数不匹配
更多回答
is it just me or i can't find the values keyword? and is that union
so that they are become several rows instead of column?
是只有我,还是我找不到Values关键字?这个联合是不是使它们变成了几行而不是列?
优秀答案推荐
Your INSERT...SELECT...
statement makes no sense. The insert lists 4 columns - (first_name, weeks_present, weeks_away, total_weeks)
but your UNION only outputs 1 column. Your column names are confusing. The PK for members
appears to be id
, but your Rolls
table has a column named roll_id
, which I would expect to be called just id
, if consistent with members
.
您的插入...选择...声明没有任何意义。插入列出了4列-(First_NAME、Week_Present、Week_Away、Total_Week),但您的Union仅输出1列。您的列名令人困惑。Members的PK看起来是id,但是Rolls表有一个名为roll_id的列,如果与Members一致,我希望它只称为id。
Based on what can be inferred from your current query, it should probably be:
根据从当前查询中可以推断出的内容,它可能应该是:
INSERT INTO attendance_report
(first_name, weeks_present, weeks_away, total_weeks)
SELECT m.first_name, SUM(r.status <> 'A'), SUM(r.status = 'A'), COUNT(*)
FROM members m
JOIN rolls r ON m.id = r.member_id
JOIN rollmappings rm ON r.roll_id = rm.id
WHERE rm.roll_year = 2023
AND m.id = 19 /* comment out this criterion to retrieve for all members */
GROUP BY m.id;
If the above query is not what you are looking for, please update your question with DDL (SHOW CREATE TABLE members;
) and sample data (markdown tables) for your members
, Rolls
and Rollmappings
tables, and the desired result.
如果以上查询不是您要查找的内容,请使用DDL(显示CREATE TABLE MEMBERS;)和Members、Rolls和Rollmappings表的示例数据(降价表)以及所需的结果更新您的问题。
更多回答
我是一名优秀的程序员,十分优秀!