Reputation:
I have 2 columns as follow:
A | B
---|---
7 | 1
7 | 2
3 | 7
4 | 5
-------
I want to get 1 column containing (1,2,3).
Currently i'm quering this:
SELECT `A` , `B`
FROM `mytable`
WHERE `A` =7
OR `B` =7
but I'm getting 2 columns containing the number 7 in both sides A and B.
i'm sure there is a way to get what I want but I don't know how to google that!!
Upvotes: 0
Views: 125
Reputation: 367
you have to try this code..
SELECT b as value FROM users WHERE a='7'
UNION All
SELECT a as value FROM users WHERE b='7'
this query must help you.
Upvotes: 0
Reputation: 37233
i think you looking for this
SELECT myresult from (
SELECT A myresult FROM Table1 WHERE A IN (1,2,3)
UNION
SELECT B myresult FROM Table1 WHERE B IN (1,2,3)
)t
ORDER BY myresult
this will out put :
MYRESULT
1
2
3
Upvotes: 1
Reputation: 31637
If you are expecting result as 1, 2, 3 use below.
SELECT colB as NewCol FROM tableName WHERE colA=7
UNION
SELECT colA as NewCol FROM tableName WHERE colB=7;
If you want both columns simply use
SELECT colA, colB FROM tableName WHERE colA=7 OR colB=7;
Upvotes: 0
Reputation: 49049
You could use this:
SELECT
case when A=7 then B else A end
FROM
yourtable
WHERE
7 IN (A, B)
If you want a single column, you could use this:
SELECT
GROUP_CONCAT(case when A=7 then B else A end
ORDER BY case when A=7 then B else A end)
FROM
yourtable
WHERE 7 IN (A, B)
See fiddle here.
If value of both A and B could be 7 at the same time, and you want to skip that row, you could substitute:
WHERE 7 IN (A, B)
with
WHERE A=7 XOR B=7
Upvotes: 3
Reputation: 40639
Try this
SELECT A as Single FROM mytable WHERE B=7 UNION
SELECT B as Single FROM mytable WHERE A=7 ORDER BY Single;
Upvotes: 0
Reputation: 7590
Not really sure what you need, but I'm guessing something like this:
SELECT A FROM mytable WHERE B=7
UNION
SELECT B FROM mytable WHERE A=7
Upvotes: 2