Solved

[MySQL+PHP] SELECT -> Ignore condition if zero rows?

Posted on 2011-03-04
3
707 Views
Last Modified: 2012-05-11
Hi,

I have this example mysql_query:

mysql_query("SELECT * FROM table WHERE id='$id' AND name NOT IN($names)");

Is there any option in mysql_query to get results even if this returns Zero rows, i.e. run query ignoring that second condition?

I know it's easy to use PHP if() but I'm just curious whether is possible to do it inside one mysql_query.

Thanks for ideas!
0
Comment
Question by:Aidam-Unlimited
3 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 250 total points
ID: 35038083
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35038438
you could do this:
mysql_query("SELECT * FROM table WHERE id='$id' AND ( name NOT IN($names) OR NOT EXISTS(  SELECT * FROM table WHERE id='$id' AND ( name NOT IN($names))  ))");

Open in new window

0
 

Author Closing Comment

by:Aidam-Unlimited
ID: 35069882
Thanks to both of you guys!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Init Waits 25 76
RDBMS and No sql database 4 36
How many transactions can mysql handle? 3 18
Preventing users manipulating URL strings 10 33
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now