mysql 查询遇到的问题

wswzwz 2015-05-14 02:57:29
有两个表,
我写了一个查询语句如下,我标红的地方既是出现问题的地方,我将person2这列去掉就能正常查询,加上person2这列就报[Err] 1222 - The used SELECT statements have a different number of columns错误,首先确定表1的person2和表2的customer_name格式是一样的,不晓得为什么?
select `g`.`id` AS `id`,1 AS `f_type`,`g`.`bank_account` AS `account`,-(`g`.`get_money`) AS `money`,`g`.`get_money_date` AS `money_date`,`g`.`becuse` AS `becuse`,`g`.`person` AS `person`,`g`.`person2` AS `person2`,`g`.`balance` AS `balance` from `get_money_log` `g` union all select `ba`.`id` AS `id`,2 AS `f_type`,`ba`.`account` AS `account`,`ba`.`receivable_money` AS `money`,`ba`.`income_date` AS `money_date`,`ba`.`note` AS `becuse`,`ba`.`income_customer` AS `person`,`ba`.`balance` AS `balance` from `business_accounts` `ba` union select `bs`.`id` AS `id`,3 AS `f_type`,`bs`.`account` AS `account`,-(`bs`.`pay_already_money`) AS `money`,`bs`.`pay_date` AS `money_date`,`bs`.`note` AS `becuse`,`bs`.`pay_person` AS `person`,`bs`.`customer_name` AS `person2`,`bs`.`balance` AS `balance` from `business_shoud` `bs`
...全文
81 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2015-05-14
  • 打赏
  • 举报
回复
一、把语句写规范化,格式化会容易排错。 二、报错信息要认真阅读,英文可以用在线翻译看懂。 [Err] 1222 - The used SELECT statements have a different number of columns 你的SELECT语句包含的列的数目不同。
select 
	`g`.`id` AS `id`,
	1 AS `f_type`,
	`g`.`bank_account` AS `account`,
	-(`g`.`get_money`) AS `money`,
	`g`.`get_money_date` AS `money_date`,
	`g`.`becuse` AS `becuse`,
	`g`.`person` AS `person`,
	`g`.`person2` AS `person2`,
	`g`.`balance` AS `balance` 
from `get_money_log` `g` 
union all 
select 
	`ba`.`id` AS `id`,
	2 AS `f_type`,
	`ba`.`account` AS `account`,
	`ba`.`receivable_money` AS `money`,
	`ba`.`income_date` AS `money_date`,
	`ba`.`note` AS `becuse`,
	`ba`.`income_customer` AS `person`,
	`ba`.`balance` AS `balance` 
from `business_accounts` `ba` 
union 
select 
	`bs`.`id` AS `id`,
	3 AS `f_type`,
	`bs`.`account` AS `account`,
	-(`bs`.`pay_already_money`) AS `money`,
	`bs`.`pay_date` AS `money_date`,
	`bs`.`note` AS `becuse`,
	`bs`.`pay_person` AS `person`,
	`bs`.`customer_name` AS `person2`,
	`bs`.`balance` AS `balance` 
from `business_shoud` `bs`

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧