您好我曾尝试在我的数据库中创建时间表视图,但我在使用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');
答案 0 :(得分:8)
这意味着在查询中访问的表中有多个名为admin_id的列,因此mysql不知道从哪个返回结果。
更改您的select语句以包含表别名(admin或timesheet),如下所示:SELECT timesheet_id, class, day, hour, week, timesheet.admin_id, date_added FROM timesheet
答案 1 :(得分:1)
由于 2 表(admin_id
和admin
)中存在timesheet
,因此如果没有,则无法在SELECT
列表中使用它表名或其别名。