V
V
vetsmen2019-08-12 14:22:45
PostgreSQL
vetsmen, 2019-08-12 14:22:45

How to create a condition on multiple columns?

There is a similar structure:
title | price1 | price2 | price3
--------------------------------- t1
24 32 55
t2 25 85 38
The condition included the price condition (from 30 to 60, for example) and the selection was similar:
title: t1, price2: 32
title: t1, price3: 55
title: t2, price3: 38
That is, that only one cost participated in the selection. If more than one value satisfies a line, you need it to be split into several lines as a result (as shown from the t1 examples)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Petr Sh, 2019-08-12
@kester

This suggests creating several queries and combining them through UNION

SELECT CONCAT('title: ', title, ', price1: ', price1)
FROM my_table
WHERE price1 BETWEEN 30 AND 60
UNION 
SELECT CONCAT('title: ', title, ', price2: ', price2)
FROM my_table
WHERE price2 BETWEEN 30 AND 60
UNION 
SELECT CONCAT('title: ', title, ', price3: ', price3)
FROM my_table
WHERE price3 BETWEEN 30 AND 60

R
Rsa97, 2019-08-12
@Rsa97

(SELECT `title`, `price1`
  WHERE ...)
UNION (SELECT `title`, `price2`
  WHERE ...)
UNION (SELECT `title`, `price3`
  WHERE ...)

And the second column of the result will be called `price1`. One column cannot have different names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question