Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MySQL - need to modify one table with contents from other table?

Posted on 2016-08-22
4
47 Views
Last Modified: 2016-08-22
Hi,
Can anyone provide an SQL query to get all distinct `location_id` from staff_locations table and add to locations_2 table as `title`.
Perhaps emptying the `locations_2` table might be the best option first?

Thanks in advance.

CREATE TABLE IF NOT EXISTS `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=21 ;



INSERT INTO `staff_locations` (`id`, `staff_id`, `location_id`) VALUES
(9, 1, 200),
(10, 1, 202),
(1, 1, 757),
(2, 1, 758),
(3, 1, 759),
(4, 1, 770),
(5, 1, 771),
(6, 1, 773),
(7, 1, 774),
(8, 1, 776),
(9, 1, 1641),
(10, 1, 1642),
(11, 2, 1636),
(12, 2, 1637),
(13, 2, 1638),
(14, 2, 1639),
(15, 2, 1641),
(16, 2, 1642),
(17, 2, 1645),
(18, 2, 1646),
(19, 2, 1647),
(20, 2, 1648);



CREATE TABLE IF NOT EXISTS `locations_2` (
  `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=203 ;



INSERT INTO `locations_2` (`id`, `title`, `position`) VALUES
(1, '200', 9999),
(2, '202', 9999);

Open in new window

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

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41766261
try this:
insert into locations_2
(title)
select location_id
from staff_locations a left join locations_2 b
on a.location_id = b.title
where b.title is null;

Open in new window

0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41766263
>>Perhaps emptying the `locations_2` table might be the best option first?
it's depends... for the comment I posted above it will only inserting the new entries into table: locations_2
0
 

Author Comment

by:sabecs
ID: 41766359
Thanks Ryan, your query works well expect I discovered I need the title from another table called locations, I will accept your solution and re post another question.
0
 

Author Closing Comment

by:sabecs
ID: 41766361
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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‚Ķ
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

809 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