A
A
atis //2015-06-09 12:59:36
SQL
atis //, 2015-06-09 12:59:36

How to pull out the id of the field from where the MIN value will be pulled out?

Something like this table

INSERT INTO `prices` (`id`, `file`, `price`) VALUES
(1, 1, 84.99),
(2, 1, 80.99),
(3, 2, 3981.27)

SELECT RAND( MIN(price), 2) AS price FROM prices

I need to pull out the minimum price for the product, as well as the id of the field where the price was pulled from.
How can this be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abdula Magomedov, 2015-06-09
@atis2345

Just put the id in the output list..
Try like this:

SELECT 
  qc.id, 
  pr.name AS producer, 
  qc.model, 
  qc.modification, 
  qc.other_info, 
  prc.id, 
  ROUND(MIN(prc.price),2) AS price 
FROM 
  `quadrocopter` qc 
  INNER JOIN `producer` pr ON pr.id = qc.producer 
  INNER JOIN `price` prc ON prc.copter_id = qc.id 
where 
  exists (
    select 
      * 
    from 
      copter_answer c 
    where 
      c.copter_id = qc.id 
      AND c.answer = 1
  ) && exists (
    select 
      * 
    from 
      copter_answer c 
    where 
      c.copter_id = qc.id 
      AND c.answer = 2
  ) && prc.price > 1000 && prc.price < 1500 
GROUP BY 
  prc.copter_id 
ORDER BY 
  qc.id DESC

A
atis //, 2015-06-09
@atis2345

SELECT
qc.id,
pr.name AS producer,
qc.model,
qc.modification,
qc.other_info,
ROUND( MIN(prc.price), 2) AS price

FROM `quadrocopter` qc

INNER JOIN `producer` pr
ON pr.id = qc.producer

INNER JOIN `price` prc
ON prc.copter_id = qc.id

where
exists (select * from copter_answer c where c.copter_id = qc.id AND c.answer = 1)
&& exists (select * from copter_answer c where c.copter_id = qc.id AND c.answer = 2)
&& prc.price > 1000
&& prc.price < 1500

GROUP BY prc.copter_id
ORDER BY qc.id DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question