3

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

选择语句

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.;

数据库代码

'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');
4

2 回答 2

11

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

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

于 2013-10-14T00:09:50.670 回答
1

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

于 2013-10-14T00:10:54.767 回答