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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement problem

Status
Not open for further replies.

s0crates9

Technical User
Jun 18, 2005
70
US
I want to be able to have a MYSQL statement which will determine to use a field if it is available. I am having problems getting this to work. I am trying to show a field if it is not null or set to zero.

An example set up:

Table 1: page [Fields: url, name, id]
Table 2: sub page [Fields: url, title, sub_id]

What I want to do is create a statement that determines if there is no sub_id, then it will skip that portion of the statement.

Example statement I've tried:
SELECT IF(sub_page.sub_id!=NULL,'',sub_page.sub_id), page.name,page.url, page.id FROM sub_page,page FROM Database;

obviously it isn't working, can anyone assist me with this issue? I really don't understand if this is possible or if it is purely boolean etc...



Business Identity and Web Development Services
 
!= is not the same as 'is not null'

why the 2 FROMs?

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
ok, I think I missed something on my explanation.

What I mean to do is similar to a php if statement like:
Code:
if(!$field){
echo "";
}else{
echo $field;
}

Except in MYSQL... Essentially what I want to accomplish is a smart decision by MYSQL to determine if the field is non-existant for an ID and if that is the case, do not use that portion of the query, rather use a query without this portion of the query.Like:

If the field is non-existant:
SELECT * FROM table WHERE field='blah';

If the field is existant:
SELECT * FROM table WHERE field='blah' AND field2='blah';

So "field2" needs to be -figured out- and negotiated if it will be in the query or not without running more SQL queries.

Thanks!

Business Identity and Web Development Services
 
If I understood right, what you are trying to achieve is, not to show null in the listing, instead you want a space. Your initial statement is correct, just change it like this.
Code:
SELECT  IF ( isnull( sub_page.sub_id ) ,  "", sub_page.sub_id ) as subpid, page.name, page.url, page.id FROM  `page` , subpage

Let me know, if you have any questions.

Surtam.

-------Information is not knowledge-------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top