Solved

MySql Query, Select distinct where... except

Posted on 2010-08-24
6
428 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
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 166 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Accepted Solution

by:
dfendig earned 167 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 38

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 167 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.

746 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

11 Experts available now in Live!

Get 1:1 Help Now