/    Sign up×
Community /Pin to ProfileBookmark

Get one of two column values

I’ve got a MySQL query. I have a column in table A which has either 1 or 2 as it’s value (always).

My query JOINS a second table. I need to get the value from column X or Z from this table.

This is decided on my 1 or 2 from table A. If it’s 1, then get the value from column X if 2 then Z.

Can this be done?

to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSADec 22.2020 — @kiwis80#1626086 Yes, this can be done. Currently where are you stuck?
Copy linkTweet thisAlerts:
@NogDogDec 22.2020 — Might be able to do it with a case statement
<i>
</i>select
a.col_1,
case
when a.col_1 = 1 then b.col_x
else b.col_z
end as meaningful_name
from table_a as a
inner join table_b as b on a.something = b.something_else

(Typed that off the top of my head, so you may need to check the manual if there are any syntax errors. ;) )

×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...