Solved

MySql Query, Select distinct where... except

Posted on 2010-08-24
6
443 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
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!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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