Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2012, 03:50 PM   PM User | #1
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Converting MS Access iif statement in SQL

Hi All,

How would I write the following MS Access query in an SQL statement:
Code:
SELECT IIf([Network_1]=-1,"Net-1",IIf([Network_2]=-1,"Net-2",IIf([Network_3]=-1,"Net-3",""))) AS Network
FROM tblOnAirActivity;
I have a table with three columns that are each true/false and I want to "merge" them such that the query will reflect one column with a unique string for each record where a given table column is true and an empty string (or nothing) where none of the table columns are true.

Thanks in advance for your help.

Kind regards,

Ken
MaDmiX is offline   Reply With Quote
Old 10-22-2012, 04:22 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
which database application are you using? All of them use SQL but syntax is different among databases.

also providing sample data and expected results will also help those you are asking to assist you. what may be clear to you is not necessarily so to others.
guelphdad is offline   Reply With Quote
Old 10-22-2012, 04:56 PM   PM User | #3
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Hi guelphdad,

I am using phpMyAdmin as my database application and mySQL for the database itself. Here is an idea of what I have and how I want the results to look:

ID Network_1 Network_2 Network_3
1 -1 0 0
2 0 0 -1
3 0 -1 0
4 -1 -1 0

This is what I want to output:

ID Network
1 Net-1
2 Net-3
3 Net-2
4 Net-1, Net-2

ID #4 only just occured to me as I was typing out this example.

Kind regards,

Ken
MaDmiX is offline   Reply With Quote
Old 10-22-2012, 09:52 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
IIF in Access is virtually identical to IF in MySQL.

You should be able to just change IIF to IF

You can't use [....] with MySQL. Use `...` instead (those are backticks, the character on same keyboard key as the ~ tilde)
Except you didn't need the [...] in Access and you don't need backticks in MySQL, either.

And you should use apostrophes instead of quotes. MySQL will take either, but quotes are not ANSI compliant.


Code:
SELECT If( Network_1=-1,'Net-1',
                         If(Network_2=-1,'Net-2',
                                         If(Network_3=-1,'Net-3','')
                         )
       ) AS Network
But your ID #4 would mean a different approach, completely.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
MaDmiX (10-23-2012)
Old 10-23-2012, 12:55 AM   PM User | #5
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Thanks Old Pedant,

That worked perfectly! I didn't realize the IF could just be substituted. As I can see the need to handle cases where more than one network will return true, what would you recommend as the best approach to handle this?

Thank you,

Ken
MaDmiX is offline   Reply With Quote
Old 10-23-2012, 01:16 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, it's pretty ugly, but you could do this:
Code:
SELECT SUBSTRING( 
           CONCAT(
                      If( Network_1 <> 0, ',Net-1',''),
                      If( Network_2 <> 0, ',Net-2',''),
                      If( Network_3 <> 0, ',Net-3','')
                 ), 
            2 ) AS Network
You see it? We concatenate ",Net-X' if the value is needed else we concatenate a blank. So we might end up with
Code:
    ',Net-1,Net-2,Net-3'
And then we use SUBSTRING to lop off the first character. (If there were no characters--if the string was just ''--substring won't complain.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-23-2012, 01:18 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh, and I would use <> 0 instead of = -1 unless the field can have other possible values.

I would bet you only got the -1 in there when you converted from Access YES/NO field to MySQL, yes? I'd be sorely tempted to change all the -1's to just 1 in the DB, but that's up to you.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-25-2012, 01:04 AM   PM User | #8
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Oh, and I would use <> 0 instead of = -1 unless the field can have other possible values.

I would bet you only got the -1 in there when you converted from Access YES/NO field to MySQL, yes? I'd be sorely tempted to change all the -1's to just 1 in the DB, but that's up to you.
You are correct. The -1 was from when I converted my Access database. This is my first web application. In the future I will definitely do things differently but I am in too deep to change now and it works fine as is.

Thanks sooo much for all of your help!!!

Ken
MaDmiX is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 AM.


Advertisement
Log in to turn off these ads.