0

I want to be able to select values from either column, where column a or b is not the value I specify.

Let me explain more.

I have a table, of buddies on a website, like this:

  columna     |    columnb
  6774                887
  887                 2423
  3434                 887
  3434                 6774
  887                 3455
  887                 33444
  343434              2343

Columna tells you who sent the request.

Our user is currently 887, how using MySQL can I find out who he is friends with?

Thanks

4
  • what is column b, rather what is the relationship between the two columns? Commented Jan 18, 2012 at 20:23
  • if you can explain what is columnb, that would help me understand what you are after. Commented Jan 18, 2012 at 20:24
  • both columns are id's of members. the first is just the person who sent the request. there is also a column status, if this is 0, then the request hasnt been aprooved yet and if its 1 then it has been aprooved and those two people are friends Commented Jan 18, 2012 at 20:29
  • Ok. So I don't think the status column is immediately relevant to your question. So if you want to get all the rows where 887 is in either columna or columnb and then get the value of the other column (that is not 887), I think you may find my answer below. Commented Jan 18, 2012 at 20:33

5 Answers 5

2
SELECT (CASE WHEN columna = 887 THEN columnb ELSE columna END) AS 'columnAlias'
FROM <table>
WHERE columna = 887
      OR columnb = 887

Edit: For using the result of the above query to get details records from another table...

Using JOIN:

SELECT T2.*
FROM Table2 AS T2
     INNER JOIN (SELECT (CASE WHEN columna = 887 THEN columnb ELSE columna END) AS 'columnAlias'
                 FROM Table1
                 WHERE columna = 887
                       OR columnb = 887) AS _Temp ON _Temp.columnAlias = T2.ForeignKeyColumn

Using Nested Query:

SELECT T2.*
FROM Table2 AS T2
WHERE T2.ForeignKeyColumn IN (SELECT (CASE WHEN columna = 887 THEN columnb ELSE columna END) AS 'columnAlias'
                              FROM Table1
                              WHERE columna = 887
                                    OR columnb = 887)
Sign up to request clarification or add additional context in comments.

6 Comments

this works! but how can i modify it further to select information based on the column which wasnt 887 from another table? thanks!
Maybe nest this query inside another... SELECT * FROM <anotherTable> WHERE someColumn IN (... <above query ...). You should be able to nest or JOIN to the same effect.
thanks for the reply, im reading up on join, im going to try and do it that way if my intelligence lets me
ok so i tried the following but got unkown column 'friend' in where clause:
SELECT (CASE WHEN buddy.penpalA = 887 THEN buddy.penpalB ELSE buddy.penpalA END) AS friend, exchange.name FROM buddy, exchange WHERE (buddy.penpalA = 887 OR buddy.penpalB = 887) AND (buddy.status = 1) AND (friend = exchange.id)
|
0

You probably need WHERE statement and the query would look like SELECT ... WHERE columna = 887 OR columnb = 887

Comments

0

Selecting multiple columns is easy

SELECT columna, columnb FROM ...

Then to exclude the results WHERE the value does not match you can use the WHERE clause

 ... WHERE (columna != 887 OR columnb != 887)

1 Comment

Wow.. you just pulled almost the entire database with EXCEPTION to buddy = 887 in either position. What about a row where ColumnA = 23 and ColumnB = 54892... that would be included in the result set...
0

The WHERE qualifies that buddy "887" is in EITHER position... you dont care. The select FIELD will get the OTHER ID... so if A = 887, then grab B, otherwise it was "B" that was 887 and get person A.

select
      if( b.ColumnA = 887, b.ColumnB, b.ColumnA ) as OtherPerson
   from
      Buddies b
   where
      887 in( b.ColumnA, b.ColumnB )

Comments

0

Make sure you unite the condition incase of more conditions:

Where (a != 1 or b != 1) and c = something

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.