MySQL 错误代码:1052 字段列表中的“admin_id”列不明确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19351633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:06:52  来源:igfitidea点击:

Error Code: 1052 Column 'admin_id' in field list is ambiguous

mysqlsqldatabasemysql-error-1052

提问by Ella Ryan

Hi I have tried to create a time sheet view in my database but I'm having trouble with the admin_id column. I'm reusing this code from another assignment which works so I'm confused t why it doesn't work. Please Help me!!!

您好,我尝试在我的数据库中创建时间表视图,但是我在使用 admin_id 列时遇到了问题。我正在从另一个有效的作业中重用这段代码,所以我很困惑为什么它不起作用。请帮我!!!

Select Statement

选择语句

SELECT timesheet_id, class, day, hour, week, admin_id, date_added FROM timesheet, day, classes, admin
WHERE timesheet_id AND
classes.class_id = timesheet.class_id AND
day.day_id = timesheet.day_id AND
admin.admin_id = timesheet.admin_id ORDER BY timesheet.timesheet_id.;

Database code

数据库代码

'CREATE DATABASE /*!32312 IF NOT EXISTS*/`timesheet` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `timesheet`;

/*Table structure for table `admin` */

DROP TABLE IF EXISTS `admin`;

CREATE TABLE `admin` (
  `admin_id` int(100) NOT NULL AUTO_INCREMENT,
  `username` varchar(10) DEFAULT NULL,
  `password` char(30) DEFAULT NULL,
  PRIMARY KEY (`admin_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

/*Data for the table `admin` */

insert  into `admin`(`admin_id`,`username`,`password`) values (1,'1627724','troll1'),(2,'1627406','troll2');

/*Table structure for table `classes` */

DROP TABLE IF EXISTS `classes`;

CREATE TABLE `classes` (
  `class_id` int(11) NOT NULL AUTO_INCREMENT,
  `class` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

/*Data for the table `classes` */

insert  into `classes`(`class_id`,`class`) values (1,'Validate and Test'),(2,'Complex Web'),(3,'Advanced OO Web'),(4,'Project Management'),(5,'Project Web'),(6,'Meeting'),(7,'Study'),(8,'Software Development');

/*Table structure for table `day` */

DROP TABLE IF EXISTS `day`;

CREATE TABLE `day` (
  `day_id` int(11) NOT NULL AUTO_INCREMENT,
  `day` varchar(15) NOT NULL,
  PRIMARY KEY (`day_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

/*Data for the table `day` */

insert  into `day`(`day_id`,`day`) values (1,'Monday'),(2,'Tuesday'),(3,'Wednesday'),(4,'Thursday'),(5,'Friday'),(6,'Saturday'),(7,'Sunday');

/*Table structure for table `menu` */

DROP TABLE IF EXISTS `menu`;

CREATE TABLE `menu` (
  `Menu_id` int(100) NOT NULL AUTO_INCREMENT,
  `Menu` char(10) DEFAULT NULL,
  PRIMARY KEY (`Menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

/*Data for the table `menu` */

insert  into `menu`(`Menu_id`,`Menu`) values (1,'index'),(2,'contact us'),(3,'project'),(4,'timesheets');

/*Table structure for table `timesheet` */

DROP TABLE IF EXISTS `timesheet`;

CREATE TABLE `timesheet` (
  `timesheet_id` int(11) NOT NULL AUTO_INCREMENT,
  `class_id` int(11) NOT NULL,
  `day_id` int(11) NOT NULL,
  `hour` float DEFAULT NULL,
  `week` varchar(8) NOT NULL,
  `admin_id` int(11) NOT NULL,
  `date_added` date NOT NULL,
  PRIMARY KEY (`timesheet_id`),
  KEY `class_fk` (`class_id`),
  KEY `day_fk` (`day_id`),
  KEY `admin_fk` (`admin_id`),
  CONSTRAINT `admin_fk` FOREIGN KEY (`admin_id`) REFERENCES `admin` (`admin_id`),
  CONSTRAINT `class_fk` FOREIGN KEY (`class_id`) REFERENCES `classes` (`class_id`),
  CONSTRAINT `day_fk` FOREIGN KEY (`day_id`) REFERENCES `day` (`day_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
/*Data for the table `timesheet`'

insert  into `timesheet`(`timesheet_id`,`class_id`,`day_id`,`hour`,`week`,`admin_id`,`date_added`) values (1,1,1,1,'week1',2,'2013-10-14'),(2,2,5,6,'week1',2,'2013-10-14'),(3,1,3,5,'week1',2,'2013-10-14'),(4,5,6,2,'week1',2,'2013-10-14'),(5,8,6,4,'week1',2,'2013-10-14');

回答by Ella Ryan

This means that there is more than one column called admin_id in the tables being accessed in your query so mysql doesn't know which to return results from.

这意味着在您的查询中访问的表中有多个名为 admin_id 的列,因此 mysql 不知道从哪个返回结果。

Change your select statement to include the table alias (either admin or timesheet) as so: SELECT timesheet_id, class, day, hour, week, timesheet.admin_id, date_added FROM timesheet

更改您的选择语句以包含表别名(管理员或时间表),如下所示: SELECT timesheet_id, class, day, hour, week, timesheet.admin_id, date_added FROM timesheet

回答by PM 77-1

Since admin_idis present in 2tables (adminand timesheet) you can not use it in SELECTlist without table nameor its alias.

由于admin_id存在于2 个表(admintimesheet)中,因此您不能在SELECT没有表名或其别名的列表中使用它。