dbTalk Databases Forums  

How do I do this?

comp.databases.mysql comp.databases.mysql


Discuss How do I do this? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
anonymous
 
Posts: n/a

Default How do I do this? - 08-24-2011 , 01:08 PM






In sort of pseudocode I want:

select ID, A from TableI
where field_B = 'a value'
and (if A not in ('value 1', 'value 2', ....)
then set A to '')

Would something like this work?

select A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: How do I do this? - 08-24-2011 , 01:23 PM






On 24-08-2011 20:08, anonymous wrote:
Quote:
In sort of pseudocode I want:

select ID, A from TableI
where field_B = 'a value'
and (if A not in ('value 1', 'value 2', ....)
then set A to '')

Would something like this work?

select A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)
no, because you want to set A to '' you will need an update:

UPDATE TableI
SET A=''
WHERE field_B='a value'
AND A NOT in ('value 1', 'value 2')



--
Luuk

Reply With Quote
  #3  
Old   
sheldonlg
 
Posts: n/a

Default Re: How do I do this? - 08-24-2011 , 01:49 PM



On 8/24/2011 2:23 PM, Luuk wrote:
Quote:
On 24-08-2011 20:08, anonymous wrote:
In sort of pseudocode I want:

select ID, A from TableI
where field_B = 'a value'
and (if A not in ('value 1', 'value 2', ....)
then set A to '')

Would something like this work?

select A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

no, because you want to set A to '' you will need an update:

UPDATE TableI
SET A=''
WHERE field_B='a value'
AND A NOT in ('value 1', 'value 2')
Sorry, I wasn't clear. I don't want to set A in the table to ''. That
is unchanged. What I want is for the select statement to return the
table's value of A if it is in the provided list, otherwise to return a
'' -- and both with the associated ID.

I will then be showing a list of IDs along with the associated values of
A. The problem is that some of those values of A are invalid, so I want
to force the user to pick a valid one from the list that I would
provide. I can do that by blanking out the value derived from the table
if it is invalid.

--
Shelly

Reply With Quote
  #4  
Old   
anonymous
 
Posts: n/a

Default Re: How do I do this? - 08-24-2011 , 01:50 PM



On 8/24/2011 2:23 PM, Luuk wrote:
Quote:
On 24-08-2011 20:08, anonymous wrote:
In sort of pseudocode I want:

select ID, A from TableI
where field_B = 'a value'
and (if A not in ('value 1', 'value 2', ....)
then set A to '')

Would something like this work?

select A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

no, because you want to set A to '' you will need an update:

UPDATE TableI
SET A=''
WHERE field_B='a value'
AND A NOT in ('value 1', 'value 2')



Sorry, I wasn't clear. I don't want to set A in the table to ''. That
is unchanged. What I want is for the select statement to return the
table's value of A if it is in the provided list, otherwise to return a
'' -- and both with the associated ID.

I will then be showing a list of IDs along with the associated values of
A. The problem is that some of those values of A are invalid, so I want
to force the user to pick a valid one from the list that I would
provide. I can do that by blanking out the value derived from the table
if it is invalid.

Reply With Quote
  #5  
Old   
Luuk
 
Posts: n/a

Default Re: How do I do this? - 08-24-2011 , 03:05 PM



On 24-08-2011 20:50, anonymous wrote:
Quote:
On 8/24/2011 2:23 PM, Luuk wrote:
On 24-08-2011 20:08, anonymous wrote:
In sort of pseudocode I want:

select ID, A from TableI
where field_B = 'a value'
and (if A not in ('value 1', 'value 2', ....)
then set A to '')

Would something like this work?

select A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

no, because you want to set A to '' you will need an update:

UPDATE TableI
SET A=''
WHERE field_B='a value'
AND A NOT in ('value 1', 'value 2')



Sorry, I wasn't clear. I don't want to set A in the table to ''. That
is unchanged. What I want is for the select statement to return the
table's value of A if it is in the provided list, otherwise to return a
'' -- and both with the associated ID.

I will then be showing a list of IDs along with the associated values of
A. The problem is that some of those values of A are invalid, so I want
to force the user to pick a valid one from the list that I would
provide. I can do that by blanking out the value derived from the table
if it is invalid.

If you do a UNION, both parts should return the same number of fields.

select ID, A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

but this can be done also like this:

SELECT
ID, IF (A IN ('value 1', 'value 2'),A,'') as A
FROM TableI
where field_B = 'a value'


--
Luuk

Reply With Quote
  #6  
Old   
anonymous
 
Posts: n/a

Default Re: How do I do this? - 08-24-2011 , 04:08 PM



On 8/24/2011 4:05 PM, Luuk wrote:
Quote:
On 24-08-2011 20:50, anonymous wrote:
On 8/24/2011 2:23 PM, Luuk wrote:
On 24-08-2011 20:08, anonymous wrote:
In sort of pseudocode I want:

select ID, A from TableI
where field_B = 'a value'
and (if A not in ('value 1', 'value 2', ....)
then set A to '')

Would something like this work?

select A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

no, because you want to set A to '' you will need an update:

UPDATE TableI
SET A=''
WHERE field_B='a value'
AND A NOT in ('value 1', 'value 2')



Sorry, I wasn't clear. I don't want to set A in the table to ''. That
is unchanged. What I want is for the select statement to return the
table's value of A if it is in the provided list, otherwise to return a
'' -- and both with the associated ID.

I will then be showing a list of IDs along with the associated values of
A. The problem is that some of those values of A are invalid, so I want
to force the user to pick a valid one from the list that I would
provide. I can do that by blanking out the value derived from the table
if it is invalid.


If you do a UNION, both parts should return the same number of fields.
My example does that. Not the "'' as A"

Quote:
select ID, A from TableI
where field_B = 'a value'
and A IN ('value 1', 'value 2', ....)
union all
select ID, '' as A from TableI
where field B = 'a value'
and A NOT IN ('value 1', 'value 2', ....)

but this can be done also like this:

SELECT
ID, IF (A IN ('value 1', 'value 2'),A,'') as A
FROM TableI
where field_B = 'a value'
Nice. Thanks. That is the syntax I was looking for.

Reply With Quote
  #7  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: How do I do this? - 08-25-2011 , 01:25 AM



El 24/08/2011 22:05, Luuk escribió/wrote:
Quote:
but this can be done also like this:

SELECT
ID, IF (A IN ('value 1', 'value 2'),A,'') as A
FROM TableI
where field_B = 'a value'
Another possibility:

SELECT ID,
CASE
WHEN A NOT IN ('value 1', 'value 2')
THEN ''
ELSE A
END AS A
FROM TableI
WHERE field_B = 'a value'

In this example it's equivalent. The main difference is that CASE...END
can be generalised to N conditions (and it's also portable to other DBMS).


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #8  
Old   
anonymous
 
Posts: n/a

Default Re: How do I do this? - 08-25-2011 , 06:36 AM



On 8/25/2011 2:25 AM, "Álvaro G. Vicario" wrote:
Quote:
El 24/08/2011 22:05, Luuk escribió/wrote:
but this can be done also like this:

SELECT
ID, IF (A IN ('value 1', 'value 2'),A,'') as A
FROM TableI
where field_B = 'a value'

Another possibility:

SELECT ID,
CASE
WHEN A NOT IN ('value 1', 'value 2')
THEN ''
ELSE A
END AS A
FROM TableI
WHERE field_B = 'a value'

In this example it's equivalent. The main difference is that CASE...END
can be generalised to N conditions (and it's also portable to other DBMS).


Thanks. That is actually what I wound up with after further investigation.

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.