Question

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!!!

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');
Was it helpful?

Solution

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.

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

OTHER TIPS

Since admin_id is present in 2 tables (admin and timesheet) you can not use it in SELECT list without table name or its alias.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top