LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2018, 08:35 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
MariaDB - Error 1093: Table 'xxx' is specified twice ...


Hi,

I have two tables invoice and materials. I want to UPDATE the materials field 'mdate' with the invoices field 'datepick' WHERE the materials field 'invno' = the invoice field 'invno'. My query and the results are shown below.

NOTE: I have seen solutions for this but they are all mixed with complex statements that are very confusing to me. I hope that someone could show me a 'simple' work around for this problem.

Thanks R



Code:
MariaDB [sagle]> UPDATE materials SET mdate=( SELECT invoice.datepick FROM invoice, materials WHERE materials.invno=invoice.invno);
ERROR 1093 (HY000): Table 'materials' is specified twice, both as a target for 'UPDATE' and as a separate source for data
 
Old 07-26-2018, 09:07 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
It would be helpful if you could show us the actual table structure and explain exactly what you mean by "complex" statements.

That said, from what you have posted I think what you really want to use is a JOIN of the two tables, something like this:

Code:
UPDATE materials JOIN invoice USING(invno) SET mdate=datepick;
This is not the same version, but the syntax should be identical: MySQL UPDATE reference

Last edited by astrogeek; 07-27-2018 at 02:14 AM. Reason: typo, add ref link
 
Old 07-27-2018, 10:58 AM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks Astrogeek,

Works fine! Does the JOIN ... USING work with INSERT and SELECT also?

R
 
Old 07-27-2018, 02:55 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
JOIN is a key concept in the relational model and applies to pretty much any multi-table query. JOIN is "product", or multiplication in the relational algebra.

Relational algebra is to relations as arithmetic is to numbers. What we call "tables" are really "relations" from SET theory, and SQL implements (somewhat poorly) the algebra which operates on them. To use it effectively you need to learn the relational algebra much as you must learn arithmetic to balance your checkbook. It really isn't difficult and more than worth the effort!

JOIN does the heavy lifting, WHERE is like a filter to apply after the fact, if necessary.

When you find yourself attempting to express something with WHERE which becomes very complex or doesn't seem to do what you want, it is very often because you are using the wrong operator! Kind of like trying to write an algebraic equation with only addition and subtraction, but without using multiplication! Sure, you could do it, but what a mess!

INSERT does not support multi table queries in MySQL/Mariadb and it is difficult to imagine an INSERT ... JOIN even conceptually.

MySQL JOIN Syntax - Note the first statement on that page:

Quote:
MySQL supports the following JOIN syntax for the table_references part of SELECT statements and multiple-table DELETE and UPDATE statements:
Now see how that applies to these...

MySQL UPDATE Syntax
MySQL SELECT Syntax
MySQL INSERT Syntax

Good luck!

Last edited by astrogeek; 07-27-2018 at 02:59 PM.
 
Old 07-30-2018, 12:48 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks astrogeek,

R
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Error, some other host already uses address XXX.XXX.XXX.XXX" mwbb_support Linux - Networking 5 01-20-2014 08:59 AM
Error, some other host already uses address xxx.xxx,xxx,xxx ryan462 Linux - Networking 20 01-24-2010 11:14 PM
http://www.spamhaus.org/query/bl?xxx.xxx.xxx.xxx (Server cannot send email now!) RMLinux Linux - Server 3 05-08-2009 02:06 AM
Error: Can't open display: xxx.xxx:0 when running X app vibbizz Linux - Networking 2 05-12-2006 11:00 AM
Host XXX.XXX.XXX.XXX is not allowed to connect to this MySQL server ocavid Linux - Newbie 2 03-16-2005 09:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration