E
E
eldar_web2019-05-23 17:30:13
SQL
eldar_web, 2019-05-23 17:30:13

How to display the cheapest among all by condition in SQL?

For example, there is a price_items table:

idmake_namecost
oneTOYOTA350
2TOYOTA100
3FIAT210
4TOYOTA190

There is a condition that you need to pull out TOYOTA where id = 1, but instead of its price it should be the cheapest among such brands, that is, TOYOTA, in fact it is 100.
Request like:
SELECT p2.cost FROM price_items AS p1 INNER JOIN price_items AS p2 ON p1.make_name = p2.make_name WHERE p1.id = 1
ORDER BY p2.cost
, doesn't work because instead of pulling out one row, it pulls out all rows with TOYOTA brands.
How to make the right request?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
coderisimo, 2019-05-23
@eldar_web

well, add LIMIT 1 :)

B
baimkin, 2019-05-24
@baimkin

SELECT cost FROM price_items WHERE name = (SELECT name FROM price_items WHERE id = 1)
ORDER BY cost LIMIT 1;

A
Alexander Lysenko, 2017-05-29
@akhur

Try like this

$re = '/\bООО\b/u';
$str = 'ООО ОООрогакопат "ООО рогопыт" рОООга "Компания ООО"';

echo preg_replace($re, '', $str);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question