SQL Query Help!
Author
Discussion

The Moose

Original Poster:

23,703 posts

240 months

Saturday 31st August 2019
quotequote all
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.

i4got

5,929 posts

109 months

Saturday 31st August 2019
quotequote all
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_id

INSERT INTO table_4 (table_1_id, table_2_id, table_3_id) select 111,id,222 from table_2




The Moose

Original Poster:

23,703 posts

240 months

Saturday 31st August 2019
quotequote all
Back of the net!

Thank you for your help