In this case i have created 2 tables:
Projects:
CREATE TABLE projects (
ID int(5) NOT NULL,
PROJECT_NAME varchar(50) NOT NULL
);
Modules:
CREATE TABLE modules (
ID_MODULE int(11) NOT NULL,
NAME varchar(50),
ID_PROJECT int(11) DEFAULT NULL
);
I've inserted data to projects table:
INSERT INTO projects (ID, PROJECT_NAME) VALUES
(1, 'BOX'),
(2, 'Duck')
Now I'd like to insert data into "modules" table, but not with ID_PROJECT. I want insert with PROJECT_NAME from "projects" table. When i tried with command( in "What have you tried?") it shew an error:
Quote:
1054 - Unknown column 'projects.PROJECT_NAME' in field list
But when i wrote like that (the second code in "What have you tried?") It didn't show any error, but it didn't insert any row record to "modules" table.
I have a question is it possible to insert into "modules" table values with inner join another table (inner join "projects" table)? If yes tell what should i do. Thx for any help.
For example:
In "modules" table i haven't insert anything. But want insert data:
ID_MODULE = 1
NAME = "S-BOX"
projects.PROJECT_NAME = "BOX" (which is inner joined on modules.ID_PROJECT = projects.ID)
What I have tried:
I've tried first with that command:
INSERT INTO modules (ID_MODULE, NAME, projects.PROJECT_NAME)
SELECT 1, 'S-BOX', projects.PROJECT_NAME
FROM modules INNER JOIN projects
ON modules.ID_PROJECT = projects.ID AND projects.PROJECT_NAME = 'BOX';
But it shew an error. But when i wrote in below command:
INSERT INTO modules (ID_MODULE, NAME, ID_PROJECT)
SELECT 1, 'S-BOX', projects.PROJECT_NAME
FROM modules INNER JOIN projects
ON modules.ID_PROJECT = projects.ID AND projects.PROJECT_NAME = 'BOX';
It didn't show any error, but it didn't insert any row record to "modules" table.