dongtanlin0765 2015-03-27 10:48
浏览 37

mysql比较产品的前两个单词并给出最低价格

database:

id  name            price

1   apple iphone    500
2   apple iphone    300
3   apple iphone    250
4   apple iphone    400
5   nokia xl        300
6   nokia xl abc    200
7   nokia xl        250
  1. i want to display product with lowest price.
  2. if product is consist of more than two words it must group by first two words and give product with lowest price.
  3. output must shows id,name and price.

my queries:

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

SELECT mt.*,TRIM( CONCAT(SPLIT_STR(`name`, " ",1)," ",SPLIT_STR(`name`, " ",2))) as shrt   
FROM items mt INNER JOIN
    (
        SELECT ID, MIN(price) MinPrice,TRIM( CONCAT(SPLIT_STR(`name`, " ",1)," ",SPLIT_STR(`name`, " ",2))) as shrt
        FROM items
        GROUP BY shrt
    ) t ON shrt = t.shrt AND mt.price = t.MinPrice

output :

id  name            price   shrt
3   apple iphone    250     apple iphone
6   nokia xl abc    200     nokia xl
7   nokia xl        250     nokia xl

Desire output :

id  name            price   shrt
3   apple iphone    250     apple iphone
6   nokia xl abc    200     nokia xl
  • 写回答

1条回答 默认 最新

  • dtrnish3637 2015-03-27 11:25
    关注

    Below is the query that fulfill the requirement. You were having an extra join and a missing group by on outer query.

    SELECT mt.* FROM items mt  JOIN(SELECT ID, MIN(price) MinPrice,TRIM( CONCAT(SPLIT_STR(`name`, " ",1)," ",SPLIT_STR(`name`, " ",2))) as shrt FROM items GROUP BY shrt) t ON  mt.price = t.MinPrice group by shrt;
    

    Added extra records to cross verify the output:- Items:-

    mysql> select * from items;
    +----+--------------------+-------+
    | id | name               | price |
    +----+--------------------+-------+
    |  1 | apple iphone       |   500 |
    |  2 | apple iphone       |   300 |
    |  3 | apple iphone       |   400 |
    |  4 | apple iphone       |   250 |
    |  5 | apple iphone       |   300 |
    |  6 | nokia x1           |   300 |
    |  7 | nokia x1 abc       |   200 |
    |  8 | nokia x1           |   250 |
    | 10 | motorolla Gx two   |   500 |
    | 11 | motorolla Gx       |   500 |
    | 12 | motorolla Gx three |   150 |
    +----+--------------------+-------+
    11 rows in set (0.00 sec)
    

    Output:-

    mysql>  SELECT mt.* FROM items mt  JOIN(SELECT ID, MIN(price) MinPrice,TRIM( CONCAT(SPLIT_STR(`name`, " ",1)," ",SPLIT_STR(`name`, " ",2))) as shrt FROM items GROUP BY shrt) t ON  mt.price = t.MinPrice group by shrt;
    +----+--------------------+-------+
    | id | name               | price |
    +----+--------------------+-------+
    |  4 | apple iphone       |   250 |
    | 12 | motorolla Gx three |   150 |
    |  7 | nokia x1 abc       |   200 |
    +----+--------------------+-------+
    3 rows in set (0.00 sec)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值