n8udd
n8udd

Reputation: 711

Using 'WHERE' with user-defined variable

I'm fairly new to SQL, and I'm trying save myself some time searching for a single value across multiple columns, so that I don't have to paste the value into 3 quotes.

EG:

SET @u1 := '000123';

SELECT * FROM database1.table1
WHERE id = @u1
OR prev_id = @u1
OR next_id = @u1
OR link_id = @u1
;

So in the above scenario, I'm trying to find where the id=000123 is either the id, prev_id, next_uid, link_uid, rather than having to paste 4 instances in the following:

SELECT * FROM database1.table1
WHERE id LIKE '000123'
OR prev_id = '000123'
OR next_id = '000123'
OR link_id = '000123'
;

Looking around, I'm fairly sure that this is possible, but perhaps I'm missing something?

Upvotes: 0

Views: 55

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

Is this what you are looking for?

SELECT *
FROM database1.table1
WHERE  @u1 in (id, prev_id, next_id, link_id);

Upvotes: 1

Related Questions