LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-25-2013, 11:20 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
mysql if exists


hello all,

I have a bit of a strange mysql query that I am trying to run, basically the pseudo code would be:

Code:
select c.id, c.name 
from companies c
where 1 = 1
and if exists(select count(*) from addresses where address = 'variable') 
    then
      and c.name = 'variable'
    else
      and c.id in (select id from another_table);
    end if;
 
Old 03-26-2013, 12:33 AM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Good to know. Glad you don't need any help with anything...

:-)

Seriously, is there a question in there somewhere? Are you asking a question???
 
Old 03-26-2013, 01:09 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Hello, yes sorry the question is; how can I get that statement to work? Is there a way?
 
Old 03-26-2013, 02:30 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
there are control flow functions in mysql:
http://dev.mysql.com/doc/refman/5.0/...functions.html

You may want to exchange the "5.0" in the link with the mysql version you are using.

Last edited by j-ray; 03-26-2013 at 02:33 AM.
 
Old 03-26-2013, 04:04 AM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Hi Cheers, I have taken a look but I am still unsure as to use it the the way that I am attempting, I dont suppose you have an example that I can use do you? Many thanks in advance....
 
Old 03-26-2013, 05:04 AM   #6
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
I'm working with scripting languages as PHP or Perl so I don't have examples that could be useful for you, sorry.
 
Old 03-28-2013, 09:48 PM   #7
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
trscookie,

One of the problems with pseudo-code is that it has no defined syntax, so it is perhaps too open to interpretation. :-)

mysql has EXISTS which can be negated as NOT EXISTS, also if statements, a case statement, which is almost like a switch statement in some languages, or a case construct in bash.

If I'm thinking correctly, and if I'm interpreting your pseudo-code correctly, I would focus on a simplified form of the conditional part of it:

Code:
if TRUE
    then
      and c.name = 'variable'
    else
      and c.id in (select id from another_table);
    end if;
I expressed it that way, to emphasize this, if the EXISTS clause is true, in a way, the clauses controlled by the if seem to evaluate to this:

Code:
and c.name = 'variable'
if false, then this clause is the result:

Code:
and c.id in (select id from another_table)
So, if the EXISTS clause is true, overall that would appear to be equivalent to this:

Code:
select c.id, c.name 
from companies c
where 1 = 1
and exists (select count(*) from addresses where address = 'variable')
and c.name = 'variable' ;
whereas if false, overall it would seem to be equivalent to this:

Code:
select c.id, c.name
from companies c
where 1 = 1
and not exists (select count(*) from addresses where address = 'variable')
and c.id in (select id from another_table) ;
I created a test database, and tables with column types that should be compatible with your SQL. With that, the last two statements I illustrated above, actually run on mysql. But from that, I'm not really clear about exactly what you are trying to accomplish.

If the statements I illustrated help you at all, then great. If they don't help, perhaps you could provide a textual, not pseudo-code description, with the specifics of what you are trying to accomplish.

Last edited by rigor; 03-28-2013 at 09:54 PM.
 
  


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
How to eliminate "Error 1086 Files already exists " in mysql sanjay87 Linux - Server 2 03-28-2012 02:56 AM
MySQL UDF lib - Function both exists and doesn't exist zolcos Linux - Server 2 10-16-2010 06:04 PM
Getting php to understand that mysql exists Red Squirrel Linux - Software 2 01-18-2006 01:21 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
MySQL db exists, can't connect to it, do I need to add user to it? Hero Doug Linux - General 7 01-09-2004 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:56 AM.

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