Solved

MySQL - need to create temporary table.

Posted on 2016-10-04
4
90 Views
Last Modified: 2016-10-08
Hi,
I am trying to create a temp table showing only `wp_ab_staff` that service `wp_ab_locations` with a specific `title`
For example for say `wp_ab_locations` `title` of '2102' only staff with id's 1 & 3 would be displayed?

Any ideas would be appreciated.

Thanks

CREATE TABLE IF NOT EXISTS `wp_ab_staff` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `wp_user_id` bigint(20) unsigned DEFAULT NULL,
  `attachment_id` int(10) unsigned DEFAULT NULL,
  `full_name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `info` text,
  `google_data` varchar(255) DEFAULT NULL,
  `google_calendar_id` varchar(255) DEFAULT NULL,
  `position` int(11) NOT NULL DEFAULT '9999',
  `visibility` enum('public','private') NOT NULL DEFAULT 'public',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;


INSERT INTO `wp_ab_staff` (`id`, `wp_user_id`, `attachment_id`, `full_name`, `email`, `phone`, `info`, `google_data`, `google_calendar_id`, `position`, `visibility`) VALUES
(1, 2, 684, 'John Doe', 'johnd@testemail.com.au', '+61444555666', 'John Doe specializes in Hair', NULL, NULL, 9999, 'public'),
(2, 3, 683, 'Andrew Smith', 'andrew@testemail.com.au', '', '', NULL, NULL, 9999, 'public'),
(3, 4, 686, 'Jackie Smith', 'jackie@testemail.com.au', '', '', NULL, NULL, 9999, 'public'),
(4, 6, 686, 'Pete Smith', 'pete@testemail.com.au', '', '', NULL, NULL, 9999, 'public');



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=126 ;


INSERT INTO `wp_ab_staff_locations` (`id`, `staff_id`, `location_id`) VALUES
(96, 1, 111),
(97, 1, 112),
(98, 1, 113),
(99, 1, 114),
(104, 3, 111),
(105, 3, 113),
(106, 3, 120),
(107, 3, 121),
(108, 3, 122),
(109, 3, 125),
(110, 3, 126),
(111, 3, 127),
(112, 3, 128),
(113, 3, 129),
(114, 3, 130);


CREATE TABLE IF NOT EXISTS `wp_ab_locations` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT '',
  `position` int(11) NOT NULL DEFAULT '9999',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2615 ;



INSERT INTO `wp_ab_locations` (`id`, `title`, `position`) VALUES
(90, '2076', 9999),
(91, '2077', 9999),
(92, '2079', 9999),
(93, '2080', 9999),
(94, '2081', 9999),
(95, '2082', 9999),
(96, '2083', 9999),
(97, '2084', 9999),
(98, '2085', 9999),
(99, '2086', 9999),
(100, '2087', 9999),
(101, '2088', 9999),
(102, '2089', 9999),
(103, '2090', 9999),
(104, '2092', 9999),
(105, '2093', 9999),
(106, '2094', 9999),
(107, '2095', 9999),
(108, '2096', 9999),
(109, '2097', 9999),
(110, '2099', 9999),
(111, '2100', 9999),
(112, '2101', 9999),
(113, '2102', 9999),
(114, '2103', 9999),
(115, '2104', 9999),
(116, '2105', 9999),
(117, '2106', 9999),
(118, '2107', 9999),
(119, '2108', 9999),
(120, '2110', 9999);

Open in new window

0
Comment
Question by:sabecs
  • 2
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41828917
You can CREATE TEMPORARY TABLE with a SELECT query embedded in the CREATE query.  There is an example in this article.  Look for "A TEMPORARY TABLE OF NEARBY ZIP CODES" in the article.
https://www.experts-exchange.com/articles/4276/What-is-near-me-Proximity-calculations-using-PHP-and-MySQL.html
0
 

Author Comment

by:sabecs
ID: 41828999
Thanks Ray, but I was trying to work out the SQL code to create the actual table.
The following seems to give me what I need but not sure if it's the correct way to do it?

CREATE TEMPORARY TABLE IF NOT EXISTS wp_ab_staff AS (
SELECT * FROM `wp_ab_staff` WHERE id IN (SELECT staff_id FROM wp_ab_staff_locations WHERE location_id = (SELECT id FROM `wp_ab_locations` WHERE `title` = '2102'))

);

SELECT * FROM `wp_ab_staff`;

Open in new window

0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41829022
Try this..SOLUTION 1

CREATE TEMPORARY TABLE IF NOT EXISTS Temp_wp_ab_staff 
AS 
(
	SELECT staff_id FROM wp_ab_staff_locations l
	INNER JOIN wp_ab_staff ws ON ws.id = l.location_id
	INNER JOIN wp_ab_locations lc ON lc.id = l.location_id
	WHERE lc.title = '2012'
);
SELECT * FROM 'Temp_wp_ab_staff';

Open in new window


or... SOLUTION 2

CREATE TEMPORARY TABLE IF NOT EXISTS Temp_wp_ab_staff AS 
(
	SELECT * FROM 'wp_ab_staff' WHERE id IN (SELECT staff_id FROM wp_ab_staff_locations WHERE location_id = (SELECT id FROM 'wp_ab_locations' WHERE 'title' = '2102'))
);
SELECT * FROM 'Temp_wp_ab_staff';

--

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41829047
The general design I would follow goes sort of like this:

1. Write the SELECT query that gives you the exact information you want to put into the TEMPORARY TABLE.  Test it thoroughly so you are comfortable with it.

2. Move the SELECT query into the sub-query of the CREATE TEMPORARY TABLE query, and re-verify that it still collects the desired information.

If I get a chance I will try loading your test data and coming up with the SELECT query, but it's getting late here.  Hopefully by tomorrow!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.

792 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