Difference between revisions of "INSERT .. SELECT"

From SQLZOO
Jump to: navigation, search
m (Reverted edits by 58.68.59.4 (talk) to last revision by 203.210.197.25)
 
Line 6: Line 6:
 
<table style='float:left' class='Bordered'>
 
<table style='float:left' class='Bordered'>
 
<caption align='center'>'''games'''</caption>
 
<caption align='center'>'''games'''</caption>
<tr><th align='center'>'''Year'''</th><th align='center'>'''City'''</th></tr>
+
<tr><th align='center'>'''yr'''</th><th align='center'>'''city'''</th></tr>
<tr><td>5000</td><td align='left'>Sydney</td></tr>
+
<tr><td>2000</td><td align='left'>Sydney</td></tr>
<tr><td>6004</td><td align='left'>Athens</td></tr>
+
<tr><td>2004</td><td align='left'>Athens</td></tr>
<tr><td>2014</td><td align='left'>Beijing</td></tr>
+
<tr><td>2008</td><td align='left'>Beijing</td></tr>
 
</table>
 
</table>
 
</table>
 
</table>

Latest revision as of 20:37, 20 May 2014

INSERT .. SELECT

The table games shows the year and the city hosting the Olympic Games.

games
yrcity
2000Sydney
2004Athens
2008Beijing
schema:scott
 DROP TABLE games
 CREATE TABLE games(
  yr INTEGER,
  city VARCHAR(20));
INSERT INTO games VALUES (2000,'Sydney');
INSERT INTO games VALUES (2004,'Athens');
INSERT INTO games VALUES (2008,'Beijing');

The INSERT SELECT statement adds a new row to the table based on a SELECT statement: In this example you run the next three Olympic games in the same three venues:

INSERT INTO games(yr,city)
  SELECT yr+12, city FROM games;
SELECT * FROM games;
INSERT INTO games(yr,city)
  SELECT yr+12, city FROM games;
SELECT * FROM games;

See also

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense