as the title says, I want to Insert into table 3 (list_mail_usr) all the rows from a column on table 1 (temp_urs) and just the very last row from 1 column in table 2, you can see, all the rows from column 1 have the same ID which is the last row from the list_id column of the list_mail_nom table.
$tmp_usr = "INSERT INTO list_mail_usr (id_usuario,list_id)
SELECT temp_urs.id_usuario
JOIN list_mail_nom.list_id
WHERE MAX(ID) FROM list_mail_nom.list_id
";
This is what I have so far. And it’s not working obviously ;(
Any suggestions?
This one almost works but insert each row from temp_usr 65 times
$tmp_usr = "INSERT INTO list_mail_usr (id_usuario,list_id)
SELECT temp_usr.id_usuario,list_mail_nom.list_id
FROM temp_usr,list_mail_nom";
$insert = $asies -> query($tmp_usr) or die (mysqli_error($asies));
You’re not using aggregate functions correctly. To do what you’re trying to do, you’re going to need to GROUP BY and use a HAVING. A much simpler query would be something like :
With large tables, MySQL’s difficulties optimizing subqueries would become apparent. If you can find the maximum ID separately & pass it in as a constant, performance will be much better against large tables (moreso large x than large y, since it’s assumed that there’s an index on your ID).