SQL Query Help!
Discussion
I haven't written SQL for a little while now, but need to write a couple of simple queries. The one that is tripping me up is this.
Table_1
id
name
email
Table_2
id
city
Table_3
id
customer_type
I then have a fourth table that is a combination of the 3:
Table_4
id
table_1_id
table_2_id
table_3_id
What I want to do is to insert into Table 4 the following:
table_1_id = {a particular id which I can specify and is set for all these inserts}
table_2_id = {every id in table 2}
table_3_id = {a particular id which I can specify and is set for all these inserts}
If I have 1,000 records in table 2, I want to insert 1,000 new records into table 4 but with table_id_1 and table_id_3 being the same for all 1,000.
Obviously SELECT id FROM table_2 gives me the list of id numbers which is great
Also I can do INSERT INTO table_4 (table_1_id, table_2_id, table_3_id) VALUES (xxx, yyy, zzz)
Combining these 2 is where I'm struggling!
What do I need to do? If it makes a difference, this is a MySQL server with PHPMyAdmin.
Table_1
id
name
Table_2
id
city
Table_3
id
customer_type
I then have a fourth table that is a combination of the 3:
Table_4
id
table_1_id
table_2_id
table_3_id
What I want to do is to insert into Table 4 the following:
table_1_id = {a particular id which I can specify and is set for all these inserts}
table_2_id = {every id in table 2}
table_3_id = {a particular id which I can specify and is set for all these inserts}
If I have 1,000 records in table 2, I want to insert 1,000 new records into table 4 but with table_id_1 and table_id_3 being the same for all 1,000.
Obviously SELECT id FROM table_2 gives me the list of id numbers which is great
Also I can do INSERT INTO table_4 (table_1_id, table_2_id, table_3_id) VALUES (xxx, yyy, zzz)
Combining these 2 is where I'm struggling!
What do I need to do? If it makes a difference, this is a MySQL server with PHPMyAdmin.
The Moose said:
I haven't written SQL for a little while now, but need to write a couple of simple queries. The one that is tripping me up is this.
Table_1
id
name
email
Table_2
id
city
Table_3
id
customer_type
I then have a fourth table that is a combination of the 3:
Table_4
id
table_1_id
table_2_id
table_3_id
What I want to do is to insert into Table 4 the following:
table_1_id = {a particular id which I can specify and is set for all these inserts}
table_2_id = {every id in table 2}
table_3_id = {a particular id which I can specify and is set for all these inserts}
If I have 1,000 records in table 2, I want to insert 1,000 new records into table 4 but with table_id_1 and table_id_3 being the same for all 1,000.
Obviously SELECT id FROM table_2 gives me the list of id numbers which is great
Also I can do INSERT INTO table_4 (table_1_id, table_2_id, table_3_id) VALUES (xxx, yyy, zzz)
Combining these 2 is where I'm struggling!
What do I need to do? If it makes a difference, this is a MySQL server with PHPMyAdmin.
Not 100% sure what you're aiming for but this will all all ids from table 2 with hard coded table_1_id and table_3_idTable_1
id
name
Table_2
id
city
Table_3
id
customer_type
I then have a fourth table that is a combination of the 3:
Table_4
id
table_1_id
table_2_id
table_3_id
What I want to do is to insert into Table 4 the following:
table_1_id = {a particular id which I can specify and is set for all these inserts}
table_2_id = {every id in table 2}
table_3_id = {a particular id which I can specify and is set for all these inserts}
If I have 1,000 records in table 2, I want to insert 1,000 new records into table 4 but with table_id_1 and table_id_3 being the same for all 1,000.
Obviously SELECT id FROM table_2 gives me the list of id numbers which is great
Also I can do INSERT INTO table_4 (table_1_id, table_2_id, table_3_id) VALUES (xxx, yyy, zzz)
Combining these 2 is where I'm struggling!
What do I need to do? If it makes a difference, this is a MySQL server with PHPMyAdmin.
INSERT INTO table_4 (table_1_id, table_2_id, table_3_id) select 111,id,222 from table_2
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


