i had a table categories(cid int primekey,name varchar,description text,parent_id,created date,updated date)
i want a script for insertion like this
insert into categories(...) values(cid value,name value,..desc, parent_Id..);
can i write the statement as
insert into categories(....) values (1,'aaa','some text',select parent_id from categories where parent_id=id,date,date);
just i wana know how can i write a select statement in insert statement
so that i can the column value through select statement.
in above querry i have to pick the parent value from cid.

Recommended Answers

All 2 Replies

You can try following, but I am not able to understand what is id in "parent_id=id".

insert into categories(....) values select 1,'aaa','some text', parent_id date,date from categories where parent_id=id;

Are you going to execute above query in php or mysql directly?

"Table"	"Create Table"
"categories1"	"CREATE TABLE `categories1` (
  `cid` int(11) NOT NULL,
  `name` varchar(30) default NULL,
  `parentid` int(11) default NULL,
  PRIMARY KEY  (`cid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1"


"cid"	"name"	"parentid"
"1"	"A"	"22"
"2"	"B"	"23"


"Table"	"Create Table"
"categories2"	"CREATE TABLE `categories2` (
  `parentid` int(11) default NULL,
  `cid` int(11) default NULL,
  `Name` varchar(30) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1"

"parentid"	"cid"	"Name"
"33"	                 \N	\N
"34"	                 \N	\N
"35"	                 \N	\N



insert into categories2 (cid, name, parentid) 
select '3', 'C', categories1.parentid from categories1 where categories1.cid = 2;


"parentid"	"cid"	"Name"
"33"	                 \N	\N
"34"	                 \N	\N
"35"	                 \N	\N
"23"	                 "3"	"C"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.