473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Duplicate values for another value in the same table

dlite922
1,584 Expert 1GB
I have a permission table that gives a userID permission to a module and the any actions within that module.

What I want to do is duplicate his permissions to another user.

Permission table has:

id (PK)
userID (FK)
module
action

When I do an insert and do a select of the values of the first user, it says It cannot use the target table in the FROM clause.

Query:
Expand|Select|Wrap|Line Numbers
  1.  
  2. INSERT INTO permission 
  3. (   userID, 
  4.     module, 
  5.     pageAction) 
  6. VALUES 
  7. (   28,
  8.     "reportModule",
  9.     (SELECT pageAction FROM permission WHERE userID = 19)
  10. );
  11.  
  12.  
  13. ERROR 1093 (HY000): You can't specify target table 'permission' for update in FROM clause
  14.  
  15.  
I've tried a couple of varieties of that same INSERT line with no luck. Is this possible?

What's interesting is the Manual is very confusing, observe these statments from the manual: http://dev.mysql.com/doc/refman/5.0/...rt-select.html

[HTML]

# The target table of the INSERT statement may appear in the FROM clause of the SELECT part of the query. (This was not possible in some older versions of MySQL.) In this case, MySQL creates a temporary table to hold the rows from the SELECT and then inserts those rows into the target table.

# Currently, you cannot insert into a table and select from the same table in a subquery.

# To avoid ambiguous column reference problems when the SELECT and the INSERT refer to the same table, provide a unique alias for each table used in the SELECT part, and qualify column names in that part with the appropriate alias.
[/HTML]


Dan
Oct 17 '08 #1
2 2905
dlite922
1,584 Expert 1GB
Resolved...by pure accident.

first I tried,

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO permission (userID, module, pageAction) VALUES ("28","reportModule", (SELECT pageAction FROM permission WHERE userID = 19));
  2. ERROR 1093 (HY000): You can't specify target table 'permission' for update in FROM clause
then out of frustration, i removed "VALUES" and it worked!


Expand|Select|Wrap|Line Numbers
  1. INSERT INTO permission (userID, module, pageAction) (SELECT "28","reportModule",pageAction FROM permission WHERE userID = 19);
  2. Query OK, 5 rows affected (0.00 sec)
  3. Records: 5  Duplicates: 0  Warnings: 0
weird huh!

hope this helps someone else!



Dan
Oct 17 '08 #2
Atli
5,058 Expert 4TB
weird huh!
If you look closely, the manual says nothing about a VALUE clause. It is only meant to be used with the *normal* version of the INSERT statement.

The INSERT ... SELECT syntax is meant to be used like so:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `myTable`(`userName`, `cola`,`colb`)
  2. SELECT 'NewUser', o.`cola`, o.`colb` FROM `myTable` AS o
  3. WHERE o.`userName` = 'OldUser';
  4.  
Which would duplicate the two columns from the old user for the new user.

... which is pretty much the same thing you did in the end there :P
Oct 17 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
4
by: JohnSouth | last post by:
Hi Within a stored procedure I'm getting the next value of a reference number using (simplified): BEGIN TRANSACTION @next_ref=select max(ref) from table insert into table (ref) values...
8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
4
by: FangQ | last post by:
hi I am very new to mysql. I have a question about using the "on duplicate update" clause with insert command. my table "data" has two columns, field1 and field2, where field1 is the index...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
4
theaybaras
by: theaybaras | last post by:
Hi All, I am creating a databse of articles and related information. I need to find a way to prevent the user from inputting duplicate information in a few forms. In one of these forms I can...
3
by: rajeshkrsingh | last post by:
Hi friends, Step1- create table duplicate ( intId int, varName varchar(50) ) insert into duplicate(intId,varName) values(1,'rajesh') insert into duplicate(intId,varName) values(2,'raj12')...
1
watertraveller
by: watertraveller | last post by:
Hi all. My ultimate goal is to return two columns, where no single value appears anywhere twice. This means that not only do I want to check that nothing from column A appears in column B and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.