选择列值为1但不是4的人员

时间:2016-04-06 19:01:09

标签: mysql

问题是:

列出已在性能ID为1而非4时购买门票的顾客的姓名。

相关表格 1)表现

SaleId > SaleTime > Total > PerformanceId > PatronId
1   2014-02-12 12:10:11 100.00  1   1
2   2014-02-12 12:10:11 40.00   1   2
3   2014-02-12 12:10:11 30.00   1   3
4   2014-02-12 12:10:11 30.00   1   null
5   2014-02-12 12:10:11 100.00  4   1
6   2014-02-12 12:10:11 40.00   4   5
7   2014-02-12 12:10:11 30.00   4   3
8   2014-02-12 12:10:11 30.00   4   null
9   2014-02-12 12:10:11 100.00  7   1
10  2014-02-12 12:10:11 40.00   7   4
11  2014-02-12 12:10:11 30.00   7   3
12  2014-02-12 12:10:11 30.00   7   null

2)赞助人

PatronId > lastName > firstName

1   Paul      Smith
2   Linda     Odom
3   Gigi      Koo
4   Kailee    Jefferson
5   Kimberly  Heart
6   boyle     Heart
7   Kimberly  Beetle
8   boyle     Beetle
9   Joe       Junior
10  Jane      Junior
11  Junior    Junior
12  Kar       Kargoolie

我的SQL代码是:

select distinct lastName, firstName
from Patron p, ticketsale t1, ticketsale t2
where p.PatronId = t1.PatronId
and p.PatronId = t2.PatronId
and t1.PerformanceId = 1
and t2.PerformanceId <> 4;

这不起作用,因为它提供3个名字(paul,linda,gigi),我应该只得到(琳达)

3 个答案:

答案 0 :(得分:2)

<强>查询

使用EXISTS和相反(不存在)的子查询:

create table patron(patronid int, lastname varchar(50), firstname varchar(50));
insert into patron values 
(1, 'Paul', 'Smith'),
(2, 'Linda', 'Odom'),
(3, 'Gigi', 'Koo'),
(4, 'Kailee', 'Jefferson'),
(5, 'Kimberly', 'Heart');

create table performance(performanceid int, patronid int);
insert into performance values
(1,1),(1,2),(1,3),(1,null),(4,1),(4,5),(4,3),(4,null),(7,1),(7,4),(7,3),(7,null);

<强>测试

请参阅SQL Fiddle

上的实时示例

我只使用表格性能中的样本数据中的顾客(其他人不需要证明这一点):

lastname | firstname
---------+----------
Linda    | Odom

我的查询返回:

const int taps = 129;
double buffer[129] = {0.0};
int offset = 0;
double input;

double coefficients[] =

{
    0.0005,0.0004,0.0002,0.0000,-0.0001,-0.0000,0.0001,0.0004,0.0007,0.0011,
    0.0015,0.0017,0.0018,0.0016,0.0011,0.0006,0.0001,-0.0003,-0.0002,0.0003,
    0.0011,0.0022,0.0032,0.0038,0.0038,0.0031,0.0015,-0.0005,-0.0026,-0.0044,
    -0.0053,-0.0050,-0.0037,-0.0016,0.0007,0.0023,0.0027,0.0012,-0.0022,-0.0072,
    -0.0127,-0.0178,-0.0212,-0.0220,-0.0199,-0.0153,-0.0093,-0.0036,-0.0001,-0.0003,
    -0.0053,-0.0147,-0.0274,-0.0408,-0.0519,-0.0573,-0.0545,-0.0419,-0.0197,0.0102,
    0.0442,0.0779,0.1064,0.1254,0.1321,0.1254,0.1064,0.0779,0.0442,0.0102,-0.0197,
    -0.0419,-0.0545,-0.0573,-0.0519,-0.0408,-0.0274,-0.0147,-0.0053,-0.0003,-0.0001,-0.0036,
    -0.0093,-0.0153,-0.0199,-0.0220,-0.0212,-0.0178,-0.0127,-0.0072,-0.0022,0.0012,0.0027,
    0.0023,0.0007,-0.0016,-0.0037,-0.0050,-0.0053,-0.0044,-0.0026,-0.0005,0.0015,0.0031,
    0.0038,0.0038,0.0032,0.0022,0.0011,0.0003,-0.0002,-0.0003,0.0001,0.0006,0.0011,
    0.0016,0.0018,0.0017,0.0015,0.0011,0.0007, 0.0004,0.0001,-0.0000,-0.0001,0.0000,
    0.0002,0.0004,0.0005
};


double filter( double input)

{
    double output = 0;


    for(int i= taps-1; i>0 ; i-- )
    {
       buffer[i] = buffer[i-1];
    }

       buffer[0] = input;


    for(int j = 0; j<taps; j++ )
    {

        output += (coefficients[j]*buffer[j]);

    }
    return output; 
}

答案 1 :(得分:2)

你走了:

SELECT firstName,
  lastName
FROM patron pat
INNER JOIN performance per1
  ON pat.PatronID = per1.PatronID AND per1.PerformanceID = 1
LEFT OUTER JOIN performance per2
  ON pat.PatronID = per2.PatronID AND per2.PerformanceID = 4
WHERE per2.PerformanceID IS NULL

刚试过ConsiderMe sqlfiddle。如果您想检查从ID 1购买门票而不是7号门票的人,可以将per2.PerformanceID上的条件更改为其他ID。等等。

如果您想了解发生了什么,请在查询中选择per1.PerformanceIDper2.PerformanceID,然后检查表格输出,然后您就会明白:

  • 您可以在不同的条件下多次连接同一个表(并避免性能较差的子查询)
  • 使用where,使用同一个表的不同别名来过滤数据

答案 2 :(得分:1)

我认为你应该简单地在单个表上执行where条件

x <- c('/travel',
       '/food and drink/restaurants',
       '/food and drink',
       '/sports/outdoors/climbing',
       '/news',
       '/family')