不显示期望的输出

时间:2018-10-10 12:37:16

标签: mysql

我做了很多搜索,但是找不到正确的答案,因为在选择查询中有多个行和列会得到以下信息:

如果要为国家/地区为泰国的行选择所有列,并且希望从列城市中跳过重复项,而仅从列总计中选择最低价格,怎么办?

我应如何申请DISTINCT比? SELECT DISTINCT合作伙伴,城市,价格,费用,总计来自%tablename%;当前不“删除”重复项。

   partner | country   | city    | price | fee | total

   W       | thailand  | bangkok | 400   | 15  | 415
   X       | vietnam   | hanoi   | 400   | 25  | 425 
   Y       | vietnam   | hanoi   | 300   | 30  | 330
   X       | thailand  | bangkok | 300   | 25  | 325
   Y       | thailand  | bangkok | 298   | 30  | 328
   Y       | thailand  | krabi   | 560   | 15  | 575
   Z       | thailand  | krabi   | 550   | 35  | 585

所以我想要以下内容:

   X       | thailand  | bangkok | 300   | 25  | 325
   Y       | thailand  | krabi   | 560   | 15  | 575

2 个答案:

答案 0 :(得分:0)

您需要使用Group By进行聚合:

SELECT t1.partner, 
       t1.country, 
       t1.city, 
       t1.price, 
       t1.fee, 
       t1.total 
FROM your_table AS t1 
JOIN (SELECT city, MIN(total) AS min_total 
      FROM your_table 
      WHERE country = 'thailand' 
      GROUP BY city) AS t2 ON t2.city = t1.city 
                          AND t2.min_total = t1.total  
GROUP BY t1.partner, 
         t1.country, 
         t1.city, 
         t1.price, 
         t1.fee, 
         t1.total  

答案 1 :(得分:0)

使用简单的co相关子查询

"use strict";
let _global = (function() {return this;})();