E
E
eldar_web2018-07-04 12:32:19
SQL
eldar_web, 2018-07-04 12:32:19

How is SQL compiled with a certain condition?

I can't think of a request.
There are two tables prices and price_items (belongs to prices - references price_id).
For these two tables there is one table of ratings - ratings with fields:
type_id: here can be id prices or price_items type_name :
' price ' or 'price_items' ball
: respectively rating if it does not have an entry, then we take ball from the parent price ? It doesn't seem to work like this:

SELECT price_ratings.ball FROM price_ratings, price_items
WHERE price_ratings.type_id IN (CASE
WHEN price_ratings.type_id != price_items.id THEN price_items.price_id
END)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-07-04
@Rsa97

SELECT `r`.`ball`
  FROM (
    (SELECT `ball`, 0 AS `order`
      FROM `ratings` 
      WHERE `type_id` = :priceItemsId
        AND `type_name` = 'price_items')
    UNION (SELECT `r`.`ball` AS `ball`, 1 AS `order`
      FROM `price_items` AS `i` 
      JOIN `ratings` AS `r`
        ON `i`.`id` = :priceItemsId
          AND `r`.`type_id` = `i`.`price_id`
          AND `r`.`type_name` = `price`)
  ) AS `r`
  ORDER BY `r`.`order`
  LIMIT 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question