0

我有一个很好的小查询。它查询单个“客户”表

这是表格:SQLFiddle 在这里:http ://sqlfiddle.com/#!2/1fcea

CREATE  TABLE `clients` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NULL ,
  `address` VARCHAR(45) NULL ,
  `state` VARCHAR(45) NULL ,
  `foo` VARCHAR(45) NULL ,
  `phone` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );

INSERT INTO`clients` (`name`, `address`, `state`, `foo`, `phone`) VALUES ('Jim', '123 Main', 'MO', '876', '2038221661');
INSERT INTO`clients` (`name`, `address`, `state`, `foo`, `phone`) VALUES ('Tom ', '234 Elm', 'MO', '433', '2038221661');
INSERT INTO`clients` (`name`, `address`, `state`, `foo`, `phone`) VALUES ('Steve', '653 Pine', 'CT', '863', '5125901977');
INSERT INTO`clients` (`name`, `address`, `state`, `foo`, `phone`) VALUES ('Dave', '654 Oak', 'NV', '872', '8769085435');
INSERT INTO`clients` (`name`, `address`, `state`, `foo`, `phone`) VALUES ('Oscar', '622 FIrst ', 'LA', '625', '5125551212');

这是查询

SELECT id, 
       name, 
       address, 
       phone 
FROM   clients 
WHERE  state IN ( 'MO', 'LA', 'CT' ) 
       AND foo > 40 
ORDER  BY foo 

它返回:

2   Tom     234 Elm     2038221661
5   Oscar   622 FIrst   5125551212
3   Steve   653 Pine    5125901977
1   Jim     123 Main    2038221661

我不希望我们的客户服务人员多次致电任何地方。所以我只需要返回不同的电话号码。

我需要它压制 (1) Jim and Return

2   Tom     234 Elm 2038221661
5   Oscar   622 FIrst   5125551212
3   Steve   653 Pine    5125901977
4

1 回答 1

1
SELECT id, 
       name, 
       address, 
       phone 
FROM   clients 
WHERE  state IN ( 'MO', 'LA', 'CT' ) 
       AND foo > 40 
       AND id in 
       (
          select min(id) from clients group by phone
       )
ORDER  BY foo 
于 2013-07-17T23:51:34.003 回答