If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > returning columns that don't have null/blank values?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-06-09, 06:37
vapor vapor is offline
Registered User
 
Join Date: May 2009
Posts: 10
returning columns that don't have null/blank values?

Hi,

I am new to DB and queries. I want to return a single row but only those columns (and data) of row that are not blank or do not have NULL value.
I don't know how to do this.

Please help.
Thanks.
Reply With Quote
  #2 (permalink)  
Old 05-06-09, 07:01
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
select my, comma, separated, column, list from mytable
where
my IS NOT NULL and
comma IS NOT NULL and
separated IS NOT NULL and
column IS NOT NULL and
<.... insert any other criteria you require ...>
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 05-06-09, 07:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vapor
I want to return a single row but only those columns (and data) of row that are not blank or do not have NULL value.
in the SELECT clause, list the columns that you want

when you receive these columns in the result set, there may be an occasional blank or NULL amongst the columns on any given row

note that the columns which have blank or NULL might vary from row to row!!

here's what you do when you encounter a blank or NULL: ignore it

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-06-09, 07:18
vapor vapor is offline
Registered User
 
Join Date: May 2009
Posts: 10
Quote:
Originally Posted by healdem
select my, comma, separated, column, list from mytable
where
my IS NOT NULL and
comma IS NOT NULL and
separated IS NOT NULL and
column IS NOT NULL and
<.... insert any other criteria you require ...>
Could it be a little more "generic"? Because in many tables I have about fifty columns each.
Reply With Quote
  #5 (permalink)  
Old 05-06-09, 08:04
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
not within SQL No, at least not as far as I know (which truth be told isn't all that much)

I suppose you may be able to use the coalesce statement and test if that is null

if you want a generic approach then consider doing it in your front end
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 05-06-09, 08:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vapor
Could it be a little more "generic"?
yes, my suggestion is as generic as it gets --

return the rows, and skip over (i.e. ignore) any column values that are blank or NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-06-09, 08:07
vapor vapor is offline
Registered User
 
Join Date: May 2009
Posts: 10
Quote:
Originally Posted by r937
yes, my suggestion is as generic as it gets --

return the rows, and skip over (i.e. ignore) any column values that are blank or NULL
Can you please help me with the query?
Thanks
Reply With Quote
  #8 (permalink)  
Old 05-06-09, 08:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vapor
Can you please help me with the query?
Thanks
sure, i'd be glad to

what is the name of the table?

which columns did you want to return?

and how do you determine which row you want?

finally, which programming language are you using to call the database from?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 05-06-09, 08:26
vapor vapor is offline
Registered User
 
Join Date: May 2009
Posts: 10
table is cities
I want to return all columns (regardless of names) that do not have null value.
I want to return first row.
and I am using Ruby to run query.
Reply With Quote
  #10 (permalink)  
Old 05-06-09, 08:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vapor
table is cities
I want to return all columns (regardless of names) that do not have null value.
I want to return first row.
and I am using Ruby to run query.
okay, this is pretty easy

here's your query:
Code:
SELECT * FROM cities LIMIT 1
then use Ruby to inspect each column, and don't display it if it contains a blank or NULL

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 05-06-09, 09:32
vapor vapor is offline
Registered User
 
Join Date: May 2009
Posts: 10
lol
so, there's no easier query for it?
because someone told me that on performance aspect it would be better to not to include null columns at first place.
Reply With Quote
  #12 (permalink)  
Old 05-06-09, 10:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vapor
someone told me that on performance aspect it would be better to not to include null columns at first place.
that someone is wrong, and does not know what he is talking about
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 05-06-09, 10:56
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
they may have a point in terms of not sending data to the client that isn't needed, but you've ruled that out by not wanting to bother with specifying the 50 columns as IS NOT NULL in your query.

mind you how you could get to 50 columns in a table called 'cities' is beyond me
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #14 (permalink)  
Old 05-06-09, 11:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by healdem
...by not wanting to bother with specifying the 50 columns as IS NOT NULL in your query.
that's not how i understood the requirement, healdem

your suggestion, to write IS NOT NULL for every column, would mean that entire rows would be dropped from the result set if even one column has a blank or NULL

the way i understood the requirement, he wants to return a single row (which one? that's not yet clear), but return only the non-blank and non-NULL column values in it -- in other words, don't return blank and NULL column values amongst the columns being returned

which is clearly not feasible
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 05-06-09, 16:37
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
yeah I guess it all comes down to interpetation

I read the question as I want to find the rows which don't contain blank columns
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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