Jump to content

[SOLVED] INSERT data with a SELECT


ainoy31

Recommended Posts

I am trying to insert data with a select statement into a table.  The tables are as follow:

 

-flight_schedule-

fs_ID int(16) NOT NULL auto_increment, fs_date varchar(32) NOT NULL, fs_deptport varchar(32) NOT NULL, fs_destport varchar(32) NOT NULL, PRIMARY KEY (fs_ID)

 

-carrier-

carrier_ID int(16) NOT NULL auto_increment, fs_ID int(16) NOT NULL, carrier_name varchar(64) NOT NULL, carrier_phone varchar(12) NOT NULL, carrier_email varchar(32) NOT NULL,

 

PRIMARY KEY (carrier_ID), FOREIGN KEY (fs_ID) REFERENCES flight_schedule (fs_ID)

 

Here is my script to insert my info:

$sql1 = "INSERT INTO flight_schedule (fs_ID, fs_date, fs_deptport, fs_destport) VALUES ('', '8/4/2007', 'KDOV', '2K3')";

mysql_query($sql1) or die("Error: " . mysql_error());

 

$sql2 = "INSERT INTO carrier SELECT * FROM flight_schedule WHERE carrier.fs_ID = flight_schedule.fs_ID;";

mysql_query($sql2) or die("Error: " . mysql_error());

 

I receive the message - Error: Unknown column 'carrier.fs_ID' in 'where clause'

Is this because I do not have carrier_ID in the flight_schedule table?

 

Much appreciation.

Link to comment
Share on other sites

 

$sql1 = "INSERT INTO flight_schedule (fs_ID, fs_date, fs_deptport, fs_destport) VALUES ('', '8/4/2007', 'KDOV', '2K3')";

I don't know why u r inserting NULL value as u have NOT NULL constraint on fs_ID column.

The length you have defined for fs_ID int(16) does not work as int can store the values between -2147483648 to 2147483647.

$sql2 = "INSERT INTO carrier SELECT * FROM flight_schedule WHERE carrier.fs_ID = flight_schedule.fs_ID;";

I don't know what u exactly want to do with that insert statement . As u have already have a foreign key referenced to fs_ID in flight_schedule so we can insert the rows that have carrier.fs_ID value that is in flight_schedule.fs_ID.Now tell what u want to do with that insert statement.INSERT with SELECT works only when the the no of columns and the datatypes of each column of both the tables match to each other.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.