INSERT INTO Return value // LAST_INSERT_ID() problem

This is a discussion on INSERT INTO Return value // LAST_INSERT_ID() problem within the MySQL Database forums, part of the Database Forums category; Hi everybody, I'm working on a relational database and am facing a problem to build a SQL query which ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-08-2008
Marijn
 
Posts: n/a
Default INSERT INTO Return value // LAST_INSERT_ID() problem

Hi everybody,

I'm working on a relational database and am facing a problem to build
a SQL query which in ONE query that updates all the necessary table
fields or fetches the appropriate data from existing entries.

I have an organization table, an email table and a user table. When I
create a user I bind that user to the specified organization and to
the specified email address. In some cases the organization or email
address already exists in the database and hence the new user should
be linked to the existing entrie. I can't use transactions due to
other reasons. My idea of a SQL for that job looks something like
this:

SELECT
IF (
`email`.`email_id` IS NOT NULL,
`email`.`email_id`,
(SELECT IF (((INSERT INTO `email` (`email_address`) VALUES
('info@example.org')) = ''), -1, LAST_INSERT_ID()))
) as `email_id`,
IF (
`organization`.`organization_id` IS NOT NULL,
`organization`.`organization_id`,
(SELECT IF (((INSERT INTO `organization`
(`organization_name`,`organization_url_name`) VALUES ('Example org',
'exampleorg')) = ''), -1, LAST_INSERT_ID()))
) as `organization_id`
FROM
`email`, `organization`
WHERE
`email`.`email_address` = 'info@example.org'
AND
`organization`.`organization_url_name` = 'exampleorg'

If the value for email_id is unequal to NULL than we select email_id
else we insert a new entry in the email table and return the
LAST_INSERT_ID. Same goes for the organization table

I understand that the problem lies within the SELECT IF(INSERT
INTO[....]) stuff but it is not entirely clear to me why..

Can somebody help me out here? Do you have all the required info? Let
me know.

Thanks in advance.

Marijn
Reply With Quote
  #2 (permalink)  
Old 01-09-2008
Willem Bogaerts
 
Posts: n/a
Default Re: INSERT INTO Return value // LAST_INSERT_ID() problem

> I have an organization table, an email table and a user table. When I
> create a user I bind that user to the specified organization and to
> the specified email address. In some cases the organization or email
> address already exists in the database and hence the new user should
> be linked to the existing entrie.


See the thread "Return Values From INSERT" just a few days ago.

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:23 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0