3

I need to compare two different columns in a mysql WHERE statement. Within a view that I have created I have a column called E_ID and one called event. I need to SELECT from the view where the E_ID != $E_ID when event = $event.

So if $event = status and $E_ID = 1 the statement would select anything thats not got the same event and the same E_ID but it could return data that has the same E_ID and a different event.

Okay lets have some examples. Lets say I have this as my two variable:

$E_ID = '1';
$event = 'status';

Okay and now in the table is this;

E_ID    event

1       status
2       status
3       status
1       gig
3       track
5       gig

As you cans the first row contains the data set in the variables so we don't want to return that. But the problem lies in E_ID as they can be the same if the event is different. So I want to return everything that does not have the E_ID of 1 when the event is status. It can however return data of the same E_ID and the same event.

What should be returned is this;

E_ID    event

2       status
3       status
1       gig
3       track
5       gig 

As you can see everything is returned but the row that has the data set in the variables.

Here's my query so far.

SELECT * FROM stream_view WHERE E_ID != '$E_ID'

Not really sure where to start so have struggled to figure it out myself

6
  • 1
    can you please make a sqlfiddle of your tables? www.sqlfiddle.com Commented Jun 9, 2013 at 14:21
  • do you want to select what is != $E_ID and = $status? Did I understand you correctly? Commented Jun 9, 2013 at 14:21
  • @fabio no I want to select what is not $E_ID WHEN event = $event. So if there are a few events such as gig, track and status it will return data that does not have the same E_ID for rows that have the same event Commented Jun 9, 2013 at 14:29
  • is event a list comma separated? Commented Jun 9, 2013 at 14:31
  • No its not it can be any value Commented Jun 9, 2013 at 14:32

3 Answers 3

1

I think the OP is more interested in this:

SELECT * FROM 
stream_view 
WHERE (event = '$event' 
AND E_ID <> '$E_ID') 
OR  event<> '$event';
Sign up to request clarification or add additional context in comments.

Comments

0

Do you mean like this?

SELECT * FROM stream_view WHERE E_ID != '$E_ID' AND event = '$event'

1 Comment

$event should be surrounded with quotes since it's not integer
0

I think you can just place both condition in your WHERE clauses

SELECT * FROM stream_view WHERE E_ID != '$E_ID' and event = '$event'

2 Comments

No actually I don't mean this. Ignore my previous comment that i've now deleted. This will select event only = to '$event'. Look at my example to see what i'm after
@nbs i took a look at your example but i didn't figure out what you are trying to achieve, could you please explain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.