Jump to content

Update multiple records using UNION


forTheDogs

Recommended Posts

Hi!  I hope this is the correct way to present this!

 

Any help would really be appreciated!

 

WAMP Server 2.0

Apache Version du serveur: 5.0.45-community-nt

Version du protocole: 10

Serveur: localhost via TCP/IP

Version du client MySQL: 5.0.45

Extensions PHP utilisées: mysqli

 

Applies ONLY to the table:

 

pedigrees

ID  RegName  SireID  Sire  DamID DAM

 

I want to update the SireID in all the records in the 'pedigrees' table that have a name in the 'Sire' field but do not have a 'SireID'

For each of these records

using the same table-pedigrees and the name in the Sire field

Find the record :  WHERE the 'RegName' is the same as the name in the 'Sire' field

Then take the 'ID' from that second record

UPDATE the 'SireID' with the 'ID' 

 

I am using phpMyAdmin and am working within the table.

I have tried using for example

 

pedigrees1.RegName instead of just RegName for all of the fields.

 

MY CODE

(SELECT RegName, ID FROM pedigrees1

WHERE SireID <1)

UNION

(SELECT Sire FROM pedigrees2

WHERE Sire != "")

UPDATE pedigrees

SET SireID = pedigrees1.ID

WHERE pedigrees1.RegName = pedigrees2.Sire

 

SQL ERROR Message

(

 

SELECT RegName, ID

FROM pedigrees1

WHERE SireID <1

)

UNION (

 

SELECT Sire

FROM pedigrees2

WHERE Sire != ""

)

UPDATE pedigrees SET SireID = pedigrees1.ID WHERE pedigrees1.RegName = pedigrees2.Sire LIMIT 0 ,

30

 

MySQL a répondu:

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE pedigrees  SET SireID = pedigrees1.ID  WHERE pedigrees1.RegName = pedigre' at line 1

 

Link to comment
Share on other sites

Thanks - can you tell me which type of join to use if you are working with just ONE Table.

 

Also, can anyone point me to a sample of a Join where just one table is being used?

 

Thanks!

How can you join something to nothing? One table, no JOIN.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.