Solved

MySQL - how to create and select temp tables?

Posted on 2016-09-16
8
73 Views
Last Modified: 2016-09-16
Hi,
I am just wondering how I would go about creating and selecting a temp MySQL table?

Can it be the same name as an existing table.

I want to reduce size of existing "wp_ab_staff_locations" table to a specific location.

Thanks in advance.

CREATE TEMPORARY TABLE IF NOT EXISTS wp_ab_staff_locations AS (SELECT * FROM wp_ab_staff_locations WHERE location_id= '754' )
SELECT * FROM wp_ab_staff_locations;


CREATE TABLE IF NOT EXISTS `wp_ab_staff_locations` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `staff_id` int(10) unsigned NOT NULL,
  `location_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_ids_idx` (`staff_id`,`location_id`),
  KEY `location_id` (`location_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=96 ;

--
-- Dumping data for table `wp_ab_staff_locations`
--

INSERT INTO `wp_ab_staff_locations` (`id`, `staff_id`, `location_id`) VALUES
(9, 1, 200),
(10, 1, 202),
(1, 1, 757),
(4, 1, 770),
(5, 1, 771),
(6, 1, 773),
(7, 1, 774),
(8, 1, 776),
(37, 3, 754),
(38, 3, 755),
(39, 3, 756),
(40, 3, 757),
(41, 3, 758),
(42, 3, 759),
(43, 3, 760),
(87, 5, 754),
(88, 5, 755),
(89, 5, 756),
(94, 5, 761),
(95, 5, 762);

Open in new window

0
Comment
Question by:sabecs
  • 4
  • 2
  • 2
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802488
>>Can it be the same name as an existing table.
why you intend to do that?

>>I want to reduce size of existing "wp_ab_staff_locations" table to a specific location.
So why you move your data into a temporarily table? Why you don't store your existing data  into an archive table (another table) instead?
0
 
LVL 24

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41802491
Here you go few sample examples..

--

CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)

CREATE TEMPORARY TABLE top10customers
SELECT p.customerNumber, 
       c.customerName, 
       FORMAT(SUM(p.amount),2) total
FROM payments p
INNER JOIN customers c ON c.customerNumber = p.customerNumber
GROUP BY p.customerNumber
ORDER BY total DESC
LIMIT 10

--

Open in new window

0
 

Author Comment

by:sabecs
ID: 41802527
Thanks for your comments, I haven't used temp tables before but from what I understand I can create for a new table for each visitor to my website specific to their location. So when they search for staff they will only see staff in their location?
0
 

Author Comment

by:sabecs
ID: 41802535
I have tried creating a temp table using phpMyAdmin form cPanel, seems to create table but I get an error when I try and view table?

CREATE TEMPORARY TABLE IF NOT EXISTS wp_ab_staff_locations AS (SELECT * FROM wp_ab_staff_locations WHERE location_id= '754' );
SELECT * FROM wp_ab_staff_locations;

 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM wp_ab_staff_locations' at line 2
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:sabecs
ID: 41802537
sorry, I had a typo, it works well.
0
 
LVL 24

Expert Comment

by:Pawan Kumar
ID: 41802540
Great ! Could you please mark one solution as accepted solution and close the question.

Thank you !
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802550
>>but from what I understand I can create for a new table for each visitor to my website specific to their location. So when they search for staff they will only see staff in their location

you NO need specifically to create temporary table for your visitor since your original table can serve the same purpose. And to store operational data (like visitor activities to your site) to temporary tables could be a risk if your MySQL Server facing error and being forced to restart the service. Your data could be gone.

just write to physical tables would be a better approach. use temporary tables for other purposes instead.
0
 

Author Closing Comment

by:sabecs
ID: 41802568
Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php date - compare 4 40
WordPress Themes 10 34
xampp tool 12 26
Website Interactive tool that will do calculations 3 29
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now