选择具有确切名称和生日的所有客户

时间:2015-07-09 14:23:31

标签: mysql sql

我想在mysql表上选择所有具有相同名称和出生日期的客户。

我的查询现在很接近,但似乎有一个缺陷:

{
 "notification": {
   "body": "HEY"
 },
 "registration_ids": [<registration token of ios device>],
 "time_to_live": 400
}

4 个答案:

答案 0 :(得分:3)

如果EXISTS另一个客户具有相同的名称和日期,则返回客户,但其他ID:

SELECT
    id,
    name,
    date
FROM
    customer c1
where exists (SELECT 1 from customer c2
              where c2.name = c1.name
                and c2.date = c1.date
                and c2.id <> c1.id)

JOIN版本:

SELECT
    c1.id,
    c1.name,
    c1.date
FROM
    customer c1
JOIN customer c2
  ON  c2.name = c1.name
  and c2.date = c1.date
  and c2.id <> c1.id

答案 1 :(得分:1)

您可以使用<div id="timer"> <input id="minutes" type="text" style="width: 90%; border: none; background-color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%;right: -5%;"> <input id="seconds" type="text" style="width: 90%; border: none; background-color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%;right: -40%;"> </div>表达式执行此操作:

EXISTS

此查询的含义可以通过假装它的简单英语来推导:我们正在寻找所有客户SELECT id, customer.name, date FROM customer c WHERE EXISTS ( SELECT * FROM customer cc WHERE cc.name=c.name AND cc.date=c.date AND cc.id <> c.id ) ,其中存在另一个客户c,其名称和出生日期相同,但不同{ {1}}。

答案 2 :(得分:1)

这样的事情应该这样做:

select
    c1.*
from 
    customer c1 
    join customer c2 on 
        c1.name = c2.name 
        and c1.birth_date = c2.birth_date 
        and c1.id != c2.id
order by name, birth_date, id
;

以下是完整的例子:

drop table customer;

create table customer (
    id int,
    name varchar(64),
    birth_date date
);

insert into customer values (1, 'Joe', '2001-01-01');
insert into customer values (2, 'Joe', '2001-01-02');
insert into customer values (3, 'Joe', '2001-01-03');
insert into customer values (4, 'Jim', '2001-01-01');
insert into customer values (5, 'Jack', '2001-01-01');
insert into customer values (6, 'George', '2001-01-01');
insert into customer values (7, 'George', '2001-01-02');
insert into customer values (8, 'Jeff', '2001-01-02');
insert into customer values (10, 'Joe', '2001-01-01');
insert into customer values (60, 'George', '2001-01-01');

select * from customer;

select
    c1.*
from 
    customer c1 
    join customer c2 on 
        c1.name = c2.name 
        and c1.birth_date = c2.birth_date 
        and c1.id != c2.id
order by name, birth_date, id
;

+ ------- + --------- + --------------- +
| id      | name      | birth_date      |
+ ------- + --------- + --------------- +
| 6       | George    | 2001-01-01      |
| 60      | George    | 2001-01-01      |
| 1       | Joe       | 2001-01-01      |
| 10      | Joe       | 2001-01-01      |
+ ------- + --------- + --------------- +
4 rows

答案 3 :(得分:0)

假设一个表只包含id,name和date,你可以在之后展开它。

解决方案将使用别名连接或exists子句。

首先设置一些代码来设置一个临时表,其中包含一些值供我们查询。

--Drop Temporary Test Table if it exists
IF OBJECT_ID('tempdb.dbo.#Customers_Test', 'U') IS NOT NULL
  DROP TABLE #Customers_Test; 

--Create a Temporary Test Table
CREATE TABLE #Customers_Test
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NULL,
    [date] [datetime] NULL,

    CONSTRAINT [PK_Customers_Test] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )
    WITH 
    (
        PAD_INDEX = OFF,
        STATISTICS_NORECOMPUTE = OFF,
        IGNORE_DUP_KEY = OFF,
        ALLOW_ROW_LOCKS = ON,
        ALLOW_PAGE_LOCKS = ON
    ) ON [PRIMARY]
) ON [PRIMARY]

--Put some temporary values into the table
INSERT INTO #Customers_Test(name, [date])
VALUES ('Joe', GETUTCDATE()),
('Mark', GETDATE()),
('Dan', '1/1/1990'),
('Dan', '1/1/1990')

现在对于实际的选择样式,有几种方法可以根据大多数偏好来执行此操作,如果此查询变得更加复杂,则可能会有一个显着的运行时优势。

--First Select Version
SELECT  #Customers_Test.id,
        #Customers_Test.name, 
        #Customers_Test.[date]
FROM #Customers_Test
WHERE EXISTS (
                SELECT 1
                FROM #Customers_Test Duped_Customers
                WHERE Duped_Customers.name = #Customers_Test.name
                AND Duped_Customers.[date] = #Customers_Test.[date]
                AND Duped_Customers.id <> #Customers_Test.id
             )

另一种方式:

--Second Select Version
SELECT  #Customers_Test.id,
        #Customers_Test.name, 
        #Customers_Test.[date]
FROM #Customers_Test
INNER JOIN #Customers_Test AS Duped_Customers
ON #Customers_Test.name = Duped_Customers.name
AND #Customers_Test.[date] = Duped_Customers.[date]
AND #Customers_Test.id <> Duped_Customers.id

最后一种方式:

--Third Select Version ( Similar to your current logic).
SELECT  #Customers_Test.id,
        #Customers_Test.name, 
        #Customers_Test.[date]
FROM #Customers_Test
WHERE #Customers_Test.name IN (
                                SELECT name
                                FROM #Customers_Test Duped_Customers
                                GROUP BY name, [date]
                                HAVING COUNT(name) > 1
                              )