Solved

MySQL - need to create temporary table.

Posted on 2016-10-04
4
52 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 108

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 17

Accepted Solution

by:
Pawan Kumar Khowal 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 108

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

705 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