I keep getting a Unknown column 'makeId' in 'field list' when I used the INSERT to take info from tables and put it in the vehicleNormal.
INSERT INTO vehicleNormal (makeId, modelId, year, cylinders, driveId, mpgHighway, mpgCity, fueltypeId)
(SELECT makeId, modelId, year, cylinders, driveId, mpgH, mpgC, fueltypeId
FROM vehicle
JOIN vehicleMake ON vehicle.make = vehicleMake.make
JOIN vehicleModel ON vehicle.model = vehicleModel.model
JOIN vehicleDrive ON vehicle.drive = vehicleDrive.drive
JOIN vehicleFuelType ON vehicle.fueltype = vehicleFuelType.fueltype);
When my table has the 'makeId'...
CREATE TABLE ‘vehicleNormal’ (
‘vehicleId’ INT NOT NULL AUTO_INCREMENT,
‘fuelType’ VARCHAR (255) DEFAULT NULL,
‘makeId’ INT (11) DEFAULT NULL,
‘modelId’ INT (11) DEFAULT NULL,
‘year’ INT (11) DEFAULT NULL,
‘cylinders’ INT (11) DEFAULT NULL,
‘driveId’ INT (11) DEFAULT NULL,
‘mpgHighway’ DECIMAL(10,2),
PRIMARY KEY (‘vehicleId’)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
Any help would be great. :/
Just added the vehicle. to all the columns in SELECT. Thank you Gordon Linoff for pointing me in the right direction.
INSERT INTO vehicleNormal (makeId, modelId, year, cylinders, driveId, mpgHighway, mpgCity, fueltypeId)
(SELECT vehicle.make, vehicle.model, vehicle.year, vehicle.cylinders, vehicle.drive, vehicle.mpgHighway, vehicle.mpgCity, vehicle.fueltype
FROM vehicle
JOIN vehicleMake ON vehicle.make = vehicleMake.make
JOIN vehicleModel ON vehicle.model = vehicleModel.model
JOIN vehicleDrive ON vehicle.drive = vehicleDrive.drive
JOIN vehicleFuelType ON vehicle.fueltype = vehicleFuelType.fueltype);
You have provided the table definition for the destination table in the INSERT, but not for the source tables. The SELECT will generate the error if a field called makeId does not exist in the source tables.
Related
I have the following two tables:
CREATE DATABASE IF NOT EXISTS springbootdb;
DROP TABLE IF EXISTS occupancy;
DROP TABLE IF EXISTS hotel;
CREATE TABLE hotel
(
id INT NOT NULL PRIMARY KEY auto_increment,
category int NOT NULL,
name TEXT NOT NULL,
owner TEXT NOT NULL,
contact TEXT NOT NULL,
address TEXT NOT NULL,
city TEXT NOT NULL,
zip TEXT NOT NULL,
phone TEXT NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE occupancy
(
id int not null primary key auto_increment,
hotelid int not null,
month int not null,
year int not null,
room_utilization int not null,
bed_utilization int not null,
room_count int not null,
bed_count int not null,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now I want to display every single hotel.id and hotel.name along with occupancy.room_count, occupancy.bed_count, occupancy.room_utilization and occupancy.bed_utilization - but only the very latest entry for each hotel.id, so the ones where occupancy.year and occupancy.month are the highest values each.
I tried a couple of things, such as
SELECT springbootdb.hotel.id, springbootdb.hotel.name, springbootdb.occupancy.bed_count, springbootdb.occupancy.bed_utilization
From springbootdb.hotel
INNER JOIN springbootdb.occupancy
ON hotel.id = occupancy.hotelid
order by springbootdb.occupancy.`year`, springbootdb.occupancy.`month` asc limit 1;
but haven't had any success unfortunately.
Can a good soul tell me how to get there?
Thanks!
This is best solved with window functions, but that requires you upgrade to MySQL 8.0.
Here's the general idea.
SELECT t.id, t.name, t.bed_count, t.bed_utilization
FROM (
SELECT h.id, h.name, o.bed_count, o.bed_utilization, ROW_NUMBER() OVER (PARTITION BY h.id ORDER BY o.`year` DESC, o.`month` DESC) AS rownum
FROM hotel AS h JOIN occupacy AS o ON h.id = o.hotelid
) AS t
WHERE t.rownum = 1;
I'm Stuck in the middle of my MySQL project. Basically for now I have three tables Room_Type, Room, Customer. Read my code for Room_Type Table and in Room Table, there's a foreign key through which I can access its room_type (Standard, deluxe, etc) and in the customer table, there's a foreign key thorough which I can access Room. But My Issue is I want to access it's room_Type too through the customer Table and show it on the customer table. Can anyone here help me out. I'm really stuck here. Please correct me if I'm missing something, It would be a great help
Thanks in Advance
Create Table Room_Type
(
rt_ID int not NULL,
room_type nvarchar(50) not NULL,
Primary key(rt_ID),
);
Create Table Room
(
room_ID int not NULL,
r_Capacity int not NULL,
room_floor int not NULL,
r_price int not NULL,
rt_ID int not NULL,
Primary key(room_ID),
Foreign Key(rt_ID) references Room_Type(rt_ID)
);
insert into Room_Type Values
(1,'Standard'),
(2,'Superior'),
(3,'Deluxe')
insert into Room Values
(1,2,0,5000,1),
(2,3,0,6000,2),
(3,1,0,3000,2)
select Room.room_ID,Room.r_capacity,Room.room_floor,Room.r_price ,Room_Type.room_type from Room inner join Room_Type ON Room.rt_ID = Room_Type.rt_ID
*/
/*Customer*/
/*
Create Table Customer
(
c_ID int not NULL,
c_name nvarchar (20) not NULL,
c_ph nvarchar(11)not NULL,
c_cnic nvarchar (13) not NULL,
c_address nvarchar (100) not NULL,
room_ID int NULL,
Primary Key(c_ID),
Foreign Key(room_ID) references Room(room_ID)
)
insert INTO Customer (c_ID,c_name,c_PH,c_cnic,c_address) Values
(1,'Hamza','337492379','327623862','ABC123');
insert INTO Customer Values
(2,'Saleem','347934723','34567890123','XYZ321',1)
I want to access it's room_Type too through the customer Table and show it on the customer table
You can join twice:
select c.*, rt.room_type
from customer c
inner join room r on r.room_id = c.room_id
inner join room_type rt on rt.rt_id = r.rt_id
I'm currently working on a project where we have to join two tables in SQL and then create a page to show the result of the two tables combined.
This is what I have so far:
SELECT *
FROM
ANIMAL
LEFT OUTER JOIN
FOOD_PORTION
ON
ANIMAL = FOOD_PORTION
and then a second page where the outcome should be:
CREATE TABLE ANIMAL(
AnimalID CHAR(5) PRIMARY KEY,
AnimalName CHAR(50) NOT NULL,
Species CHAR(50) NOT NULL,
Weight INT NOT NULL,
DOB DATE NOT NULL,
ExhibitID CHAR(5) REFERENCES EXHIBIT(ExhibitID)
);
CREATE TABLE FOOD_PORTION(
PortionSize INT NOT NULL,
AnimalID CHAR(5) REFERENCES ANIMAL(AnimalID)
);
SELECT C.Name
FROM ANIMAL AS C
UNION
SELECT S.Name
FROM FOOD_PORTION AS S
This should give you what you need. Using tadman's suggestion to change the char column to INT. Also changing the CHAR to VARCHAR columns.
I also made the ID's in each table Identity columns (so they will auto populate).
CREATE TABLE ANIMAL(
AnimalID INT PRIMARY KEY IDENTITY,
AnimalName VARCHAR(50) NOT NULL,
Species VARCHAR(50) NOT NULL,
Weight INT NOT NULL,
DOB DATE NOT NULL,
ExhibitID CHAR(5) REFERENCES EXHIBIT(ExhibitID)
);
CREATE TABLE FOOD_PORTION(
PortionSize INT IDENTITY,
AnimalID INT REFERENCES ANIMAL(AnimalID)
);
SELECT A.*, FP.*
FROM Animal A
INNER JOIN Food_Portion FP ON A.AnimalID = FP.AnimalID
I'm trying to select mltiple rows from tow table :
first table is donor
CREATE TABLE donor(
donor_number INT NOT NULL AUTO_INCREMENT,
d_name VARCHAR(30) NOT NULL,
mobile_number INT NOT NULL,
blood_group VARCHAR(20) NULL,
dob DATE NOT NULL,
gender ENUM('male','female') NOT NULL,
govid INT(10) NOT NULL,
PRIMARY KEY (donor_number )
);
second table is blood_donation
CREATE TABLE blood_donation(
donor_number INT NOT NULL,
date_of_donate DATE NOT NULL,
blood_group VARCHAR(20) NULL,
serial_number INT(10) NOT NULL,
blood_component ENUM('wb','prcb') NOT NULL,
PRIMARY KEY (donor_number , date_of_donate ),
FOREIGN KEY (donor_number) REFERENCES donor(donor_number)
);
with this select statement:
SELECT
serial_number,
blood_group
FROM blood_donation
WHERE date_of_donate = '2012-07-18'
UNION ALL
SELECT
blood_group
FROM donor
WHERE donor.donor_number=blood_donation.donor_number;
but, I get error
SQL state 42S22: Unknown column 'blood_donation.donor_number' in 'where clause'
any idea????
Actually you should not be using UNION but JOIN :)
you query will look like this
SELECT
blood_donation.serial_number,
donor.blood_group
FROM
blood_donation ,
donor
WHERE donor.donor_number = blood_donation.donor_number AND date_of_donate = '2012-07-18' ;
A UNION is used to combine more than one result set into a single result set - and each result set must have the same set of columns.
What you need is a JOIN, which is how you link multiple tables together on foreign keys etc and would be something like this:
SELECT
serial_number,
blood_group
FROM blood_donation
INNER JOIN donor ON donor.donor_number=blood_donation.donor_number
WHERE date_of_donate = '2012-07-18'
SELECT
dd.serial_number,
dd.blood_group
FROM blood_donation dd
inner join
donor d
on d.donor_number=dd.donor_number
WHERE dd.date_of_donate = '2012-07-18';
UNION is not what exactly you need, Read some more about JOINS. Also please change the selection alias of columns as per the need. And you can use Left Join instead of Inner Join if you don't want a mandatory join condition on tables.
I'm trying to count how many essays have been graded so I know how many results to display on the page. But I can't seem to get the code to work properly can someone help?
Thanks for the help in advance!
Here is what I got so far.
SELECT students.*, students_essays.*, COUNT(students_essays.id)
FROM students
INNER JOIN students_essays ON students.student_id = students_essays.student_id
INNER JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id
It should look something like the code below for my pagination.
$q = "SELECT COUNT(id) FROM students_essays";
$r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
$row = mysqli_fetch_array ($r);
$records = $row[0];
Just in case here is my MySQL tables.
CREATE TABLE students_essays (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
student_id INT UNSIGNED NOT NULL,
content TEXT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE students (
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
student_first_name VARCHAR(255) DEFAULT NULL,
student_last_name VARCHAR(255) DEFAULT NULL,
pass CHAR(40) NOT NULL,
PRIMARY KEY (student_id)
);
CREATE TABLE essays_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
grade_id INT UNSIGNED NOT NULL,
students_essays_id INT UNSIGNED NOT NULL,
student_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
letter_grade VARCHAR(2) DEFAULT NULL,
grade_points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
Here is the error message.
Error: 1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause in
Well I am guessing here based on the information at hand...
$q = "SELECT COUNT(id) FROM students_essays se INNER JOIN essays_grades eg ON se.id = eg.students_essays_id";
That would return all essays with a matching grade record.
SELECT students.*, students_essays.*, COUNT(students_essays.id)...
students.* and students_essays.* return multiple rows but COUNT(students_essays.id) would always return just one row. In my experience, MySQL returns the number of rows and the very first row only. The simplest way to do what you want might be running two separate queries: one for count and the other for fetching actual data.