?
Solved

MySql Query, Select distinct where... except

Posted on 2010-08-24
6
Medium Priority
?
453 Views
Last Modified: 2013-12-13
I have:

"SELECT DISTINCT firstName,lastName FROM tbl_users WHERE active = 'yes';"

Open in new window


I want to add an exception.... how can I block someone from appearing?
0
Comment
Question by:cstormer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33518034
SELECT DISTINCT firstName,lastName
FROM tbl_users
WHERE active = 'yes'
and not (firstname='$firstname' and lastname='$lastname')

or if you know the ID

SELECT DISTINCT firstName,lastName
FROM tbl_users
WHERE active = 'yes' and id <> $id
0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33518037
can u explain  more
is it any specific member you want to block or specific type or anything else.
0
 
LVL 1

Assisted Solution

by:bonjourjolie
bonjourjolie earned 664 total points
ID: 33518044
by which field do you want to block ?
by firstName ? or .... ?

for example

"SELECT DISTINCT firstName,lastName FROM tbl_users WHERE active = 'yes' and firstName <> 'Blocked user';"

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Accepted Solution

by:
dfendig earned 668 total points
ID: 33518059
You can add additional logic to the end of your SQL Statement. For example:

      

"SELECT DISTINCT firstName,lastName FROM tbl_users WHERE active = 'yes' AND firstname !='firstnametoexclude';"
or
"SELECT DISTINCT firstName,lastName FROM tbl_users WHERE active = 'yes' AND lastname !='lastnametoexclude';"



0
 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 668 total points
ID: 33518077
And if you want to block a whole group of users:
Where active = 'yes' and firstname not in ('a', 'b')

Or:
firstname not like 'a%'

Or:
firstname not in (select firstname from table where state = 'ca')
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33518218
> I want to add an exception.... how can I block someone from appearing?

Surely you need both first and last name, otherwise you're blocking Joe bloggs, Joe public and Joe white just by using

"SELECT DISTINCT firstName,lastName FROM tbl_users WHERE active = 'yes' and firstName <> 'Joe';"

What's wrong with the first comment?

SELECT DISTINCT firstName,lastName
FROM tbl_users
WHERE active = 'yes'
and not (firstname='$firstname' and lastname='$lastname')

e.g. if your php var is $firstname='Joe' and $lastname='Public', then the query

WHERE active = 'yes' and not (firstname='Joe' and lastname='public')

only removes joe public and leaves all other joes. The name is matched on both first+last name.
If it is possible to have similarly named people (John Brown is common), I suggested using the ID if you have it available.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

762 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