Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Insert & Joins

Tags:

join

mysql

insert

i saw this

In MySQL, joins work for INSERT, UPDATE, and DELETE statements. It's possible to change data in more than one table when joining tables in an UPDATE or DELETE statement.

in an answer for a question from the mysql certification guide. is it true? inserts with joins? an example of it?

like image 547
iceangel89 Avatar asked Sep 05 '09 09:09

iceangel89


People also ask

What is insert command in MySQL?

The INSERT command is used to add new data into a table. MySql will add a new row, once the command is executed. The date and string values should be enclosed in single quotes. The numeric values do not need to be enclosed in quotes.

How do you insert data into MySQL?

To insert data into a MySQL table, you would need to use the SQL INSERT INTO command. You can insert data into the MySQL table by using the mysql> prompt or by using any script like PHP.

How do I add values to a specific column in MySQL?

First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.

What Is syntax for insert?

There are two basic syntax of INSERT INTO statement is as follows: INSERT INTO TABLE_NAME (column1, column2, column3,... columnN)] VALUES (value1, value2, value3,... valueN);


2 Answers

You can INSERT ... SELECT with mysql, which is probably what they mean. For example:

INSERT INTO tableNew (col1, col2)
  SELECT tbl1.col1, tbl2.col2
  FROM tbl1 JOIN tbl2
like image 110
Marius Avatar answered Sep 22 '22 09:09

Marius


To complete the set, here's one for DELETE. This is a common method for deleting rows together with their dependencies without triggers.

DELETE users, comments
FROM users JOIN comments ON comments.author=users.id
WHERE users.isspammer=1
like image 39
bobince Avatar answered Sep 19 '22 09:09

bobince