1

I have a MySQL query left joining two tables. This is the current query result:

id | login | privacy-weight | requires
--------------------------------------------
0  | user  | 1              | NULL
0  | user2 | 1              | NULL
0  | user3 | 1              | privacy-weight

The query itself is not important, as I'd only like to add a WHERE condition to the query as it is now.

I need to fetch only values which (in my own words):

IF (`requires` = 'privacy-weight'), then `privacy-weight` must equal = 0;

That means, I need this condition:

WHERE `privacy-weight` = 0

BUT only if this is true:

requires = 'privacy-weight'

Can this be done?

EDIT

Obviously this is too difficult to understand, therefore, an example output:

privacy-weight | requires
-------------------------
0              | NULL
1              | NULL
0              | privacy-weight
1              | NULL

These would be ignored (not fetched):

privacy-weight | requires
-------------------------
1              | privacy-weight
10
  • And if requires <> 'privacy-weight' what do you expect? Commented Jan 30, 2013 at 16:03
  • @Muatik: no, I didn't. that's not the condition I described. Commented Jan 30, 2013 at 16:03
  • what's the point having privacy-weight column then as it's basically based on requires column ? Commented Jan 30, 2013 at 16:04
  • This question confuses WHERE clauses and IF statements which are too very different things. What rows do you actually want to output? Commented Jan 30, 2013 at 16:14
  • @Cfreak: I will edit my question. Commented Jan 30, 2013 at 16:16

6 Answers 6

3

You could use this simple test:

WHERE (requires = 'privacy-weight' AND privacy-weight = 0) OR requires <> 'privacy-weight' OR requires IS NULL

The first part (requires = 'privacy-weight' AND privacy-weight = 0) prevents the output of:

privacy-weight | requires
-------------------------
1              | privacy-weight

But keeps:

privacy-weight | requires
-------------------------
0              | privacy-weight

While the second part OR requires <> 'privacy-weight' OR requires IS NULL will keep the following ones:

privacy-weight | requires
-------------------------
0              | NULL
1              | NULL
1              | NULL
Sign up to request clarification or add additional context in comments.

1 Comment

Although not very sophisticated, I think this is the only solution, unfortunately. Accepting for being correct, yielding proper results.
1

I think this should work:

SELECT id, login, ( IF(requires='privacy-weight',0,privacy-weight) ) AS privacy-weight, requires
FROM [mytable]
WHERE [mywhereclause]

For more information on how the IF function works in MySQL see the docs at http://dev.mysql.com/doc/refman/5.5/en/if.html

3 Comments

I don't think that's what he wants. That will still output the rows where the requires is some other value
Then he can limit within his where clause. This actually outputs 0 into the column based on the value of another column, which is what he asked for. In my opinion this is more flexible for future reuse of the query.
All he needs to do is limit it in his where clause. There's no reason for an if() statement at all
1
WHERE CASE WHEN requires = 'privacy-weight' THEN 0 ELSE privacy-weight END = 0 

3 Comments

Any explanation of those ZEROes, please?
This still outputs the rows that he doesn't want.
Also, this doesn't seem to return values when privacy-weight is NULL
1

simply:

WHERE NOT (requires='privacy-weight' AND privacy-weight<>0)

1 Comment

Try to add an explanation instead of just 'simply'... Why does this solve the problem and in what way.
0

There is an IF() function in mysql as @philwinkle posted but it changes the values of the columns, it doesn't affect the rows. You want to add this to your WHERE statement.

SELECT id, login, `privacy-weight`, requires FROM yourtable 
  WHERE `privacy-weight`=0 AND requires='privacy-weight'

Edit I've changed this back to answer the OP's question as written.

8 Comments

+1 if the intent is to limit the query only to privacy-weights.
Uhm, but I also want values where requires is NULL. This ignores those.
Then you should specify that in your question. I've edited the query to account for null
Still wrong. I want values where privacy-weight is 2. The new query ignores those.
Then your question is wrong. You specifically state you want rows where privacy-weight=0 when requires='privacy-weight'
|
0

You can do if condition in where clause

SELECT *
FROM mytable
WHERE IF(requires = 'privacy-weight',privacy-weight = 0,1);

But you can achieve it without if too as you have mentioned you ONLY need

SELECT *
FROM mytable
WHERE requires = 'privacy-weight'
    AND privacy - weight = 0;

Comments

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.