Using Temporary Tables : Temporary Table « Table Index « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Table Index » Temporary Table 
Using Temporary Tables
 
/*
mysql> INSERT INTO tmp
    ->    SELECT Student.Name AS StudentName, AVG(Mark) AS AverageMark
    ->    FROM StudentExam
    ->       INNER JOIN Student
    ->       ON StudentExam.StudentID = Student.StudentID
    ->    GROUP BY Student.Name;
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from tmp;
+-------------+-------------+
| StudentName | AverageMark |
+-------------+-------------+
| Cory But    |          44 |
| Joe Wang     |          64 |
+-------------+-------------+
2 rows in set (0.00 sec)

mysql> SELECT StudentName, AverageMark FROM tmp WHERE AverageMark < 50;
+-------------+-------------+
| StudentName | AverageMark |
+-------------+-------------+
| Cory But    |          44 |
+-------------+-------------+
1 row in set (0.00 sec)


*/

/* Create Student and StudentExam TABLE  */
Drop TABLE Student;
Drop TABLE StudentExam;

CREATE TABLE Student (
   StudentID INT NOT NULL PRIMARY KEY,
   Name      VARCHAR(50NOT NULL
)TYPE = InnoDB;

CREATE TABLE StudentExam (
   StudentID  INT NOT NULL,
   Mark       INT,
   Comments   VARCHAR(255),

   CONSTRAINT FK_Student FOREIGN KEY (StudentID)
              REFERENCES Student(StudentID)

)TYPE = InnoDB;



/* Insert Data*/
INSERT INTO Student (StudentID,NameVALUES (1,'John Jones');
INSERT INTO Student (StudentID,NameVALUES (2,'Gary Burton');
INSERT INTO Student (StudentID,NameVALUES (3,'Emily Scarlett');

INSERT INTO StudentExam (StudentID,Mark,CommentsVALUES (1,55,'Java');
INSERT INTO StudentExam (StudentID,Mark,CommentsVALUES (1,73,'C#');
INSERT INTO StudentExam (StudentID,Mark,CommentsVALUES (2,44,'JavaScript');

/* Using Temporary Tables */  
CREATE TEMPORARY TABLE tmp (StudentName VARCHAR(50), AverageMark INT);

INSERT INTO tmp
   SELECT Student.Name AS StudentName, AVG(MarkAS AverageMark
   FROM StudentExam
      INNER JOIN Student
      ON StudentExam.StudentID = Student.StudentID
   GROUP BY Student.Name;

select from tmp;


SELECT StudentName, AverageMark FROM tmp WHERE AverageMark < 50;
SELECT StudentName, AverageMark FROM tmp WHERE AverageMark > 70;

           
         
  
Related examples in the same category
1.Give ordinary users the ability to lock (LOCK) tables, create temporary tables, and execute stored procedures.
2.Copying to a Temporary Table
3.Creating Temporary Tables
4.A temporary table can be created by adding the keyword TEMPORARY to the CREATE TABLE statement:
5.CREATE TEMPORARY TABLE
6.Duplicate table name between normal table and temporary table
7.Memory engine for temporary table
8.Create a TEMPORARY TABLE;
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.