如何使用内部联接查询和where like子句从表Produto_precos中获取其他两个值

时间:2019-01-19 20:20:32

标签: mysql sql hibernate

我有两个表ProductProduct_prices。对于每种产品,我有三种价格类型(ebook(0)impress(1)combo(2))。我想加入两个表并获得该商品的三种价格。当我执行子句WHERE prices1_.value like '%1%'

餐桌产品

------------------------------------------------
id    description        pages  title
------------------------------------------------
1     Harry Potter        230    harry Potter
2     Lord of The rings   950    Lord of the rings
3     game of thrones     980    game of thrones

表Product_prices

------------------------------------------------
Product_id bookType  value
------------------------------------------------
1          0           20.40 
1          1           28.00
1          2           40.00
2          0           15.00
2          1           25.50
2          2           42.00
3          0           21.00
3          1           30.50
3          2           47.00

关于休眠的查询:


    select 
    distinct 
    product0_.id as id1_0_,
    product0_.description as descript3_0_, 
    product0_.pages as pages4_0_, 
    product0_.title as title6_0_, 
    prices1_.Product_id as Product_1_1_0__, 
    prices1_.bookType as bookType2_1_0__, 
    prices1_.value as value3_1_0__ 
    from Produto produto0_ 
    inner join Product_prices prices1_ on product0_.id=prices1_.Product_id where lower(product0_.title) like '%1%' 
    or prices1_.bookType = 0 and prices1_.value like '%1%'
    or prices1_.bookType = 1 and prices1_.value like '%1%'
    or prices1_.bookType = 2 and prices1_.value like '%1%'

我想得到以下结果,或者只给出一行,每个产品的三个值分别为

------------------------------------------------
id    description        pages  title Product_id bookType  value
-----------------------------------------------------------------
2     Lord of The rings   950 Lord of...  2         0       15.00
2     Lord of The rings   950 Lord of...  2         1       25.00
2     Lord of The rings   950 Lord of...  2         2       42.00
3     game of thrones     980 game of...  3         0       21.00
3     game of thrones     980 game of...  3         1       30.50
3     game of thrones     980 game of...  3         2       47.00

但是我只有以下结果:

------------------------------------------------
id    description        pages  title Product_id bookType  value
-----------------------------------------------------------------
2     Lord of The rings   950 Lord of...  2         0       15.00
3     game of thrones     980 game of...  3         0       21.00

2 个答案:

答案 0 :(得分:0)

我认为您可以通过条件聚合来完成自己想做的事情

select p.id, p.description, p.pages, p.title,
       max(case when pp.type = 0 then pp.value end) as ebook_price,
       max(case when pp.type = 1 then pp.value end) as impress_price,
       max(case when pp.type = 2 then pp.value end) as combo_price
from Produto p inner join
     Product_prices pp
     on p.id = pp.Product_id
where lower(p.title) like '%1%' 
group by p.id, p.description, p.pages, p.title;

答案 1 :(得分:0)

提醒我,以下问题不能解决问题的哪一部分...

DROP TABLE IF EXISTS product;

CREATE TABLE product
(id SERIAL PRIMARY KEY
,title VARCHAR(30) NOT NULL
,pages INT NOT NULL
);

INSERT INTO product VALUES
(1,'Harry Potter',230),
(2,'Lord of the Rings',950),
(3,'Game of Thrones',980);

DROP TABLE IF EXISTS prices;

CREATE TABLE prices
(product_id INT NOT NULL
,book_type INT NOT NULL
,price DECIMAL(5,2) NOT NULL
,PRIMARY KEY(product_id,book_type)
);

INSERT INTO prices VALUES
(1,0,20.40),
(1,1,28.00),
(1,2,40.00),
(2,0,15.00),
(2,1,25.50),
(2,2,42.00),
(3,0,21.00),
(3,1,30.50),
(3,2,47.00);

SELECT c.*
     , a.*
  FROM prices a
  JOIN prices b
    ON b.product_id = a.product_id
  JOIN product c
    ON c.id = b.product_id
 WHERE b.price LIKE '%1%';
+----+-------------------+-------+------------+-----------+-------+
| id | title             | pages | product_id | book_type | price |
+----+-------------------+-------+------------+-----------+-------+
|  2 | Lord of the Rings |   950 |          2 |         0 | 15.00 |
|  2 | Lord of the Rings |   950 |          2 |         1 | 25.50 |
|  2 | Lord of the Rings |   950 |          2 |         2 | 42.00 |
|  3 | Game of Thrones   |   980 |          3 |         0 | 21.00 |
|  3 | Game of Thrones   |   980 |          3 |         1 | 30.50 |
|  3 | Game of Thrones   |   980 |          3 |         2 | 47.00 |
+----+-------------------+-------+------------+-----------+-------+