Solved

MySql Query, Select distinct where... except

Posted on 2010-08-24
6
449 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 39

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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