Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return only NOT NULL columns in mysql? 1

Status
Not open for further replies.

Actorial

Programmer
Aug 23, 2006
38
0
0
US
Hi,

I'm wondering how to return the names of the columns that are not null in a mySQL query. Basically I have BLOBs in a database and don't want to return the data yet, just want to know whether there is data present in that column for a particular row, and then return the name of that column.

I've tried a few things and no luck. I've done some research is just doesn't seem to get me anywhere. Any help out there?
 
Do you mean something along the lines of:
[tt]
SELECT
IF(col1 IS NULL,'','col1') c1,
IF(col2 IS NULL,'','col2') c2
FROM tblname
WHERE id=1234
[/tt]
 
Yes! That did it. I used a statement like this:

Code:
          <?
$queryC = "SELECT IF(blobData IS NULL, 0, 1) as c1, IF(blobData2 IS NULL, 0, 1) as c2, IF(blobData3 IS NULL, 0, 1) as c3, IF(blobData4 IS NULL, 0, 1) as c4 FROM blobs WHERE blobId='$id' LIMIT 1";  
$resultC = mysql_query($queryC) or die('Error, query3 failed. ' . mysql_error()); 
if(mysql_num_rows($resultC) != 0){
	while($rowC = mysql_fetch_array($resultC)) {  
        list($b1, $b2, $b3, $b4) = $rowC; 
	}
}
$num_blobs = $b1 + $b2 + $b3 + $b4;

Probably not the most efficient, but it did the trick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top