Click here to Skip to main content
15,896,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 21-May-19 3:29am

1 solution

You cannot insert data into a column which doesn't exist.

The modules table doesn't have a column called PROJECT_NAME, so you cannot insert anything into that column.

The ID_PROJECT column expects a number. The project name is not a number. You cannot insert the project name into that field.

Both commands attempt to join to the modules table. But that table doesn't contain any rows, so there will be no rows to insert. You need to refresh your understanding of how SQL joins work:
Visual Representation of SQL Joins[^]

Your command should look something like this:
SQL
INSERT INTO modules (ID_MODULE, NAME, ID_PROJECT)
SELECT 1, 'S-BOX', projects.ID
FROM projects
WHERE projects.PROJECT_NAME = 'BOX';
 
Share this answer
 
Comments
Member 10696161 22-May-19 7:08am    
Ok now i know what should i do next time. Thx for help! If only i don't why my question is downvoted?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900