SQL语言SQL查询

时间:2015-02-27 03:20:11

标签: sql

不确定我的sql语言是否正确但无论如何,假设有3个实体,如下所示

customer(name, passport, nationality, age)
menu(type, item, price)
breakfast(passport date, item)

为什么以下sql查询可能无法正确找到早餐订购炒饭的客户的平均年龄?

SELECT AVG(c.age)
FROM customer c, breakfast b
WHERE c.passport=b.passport
AND b.item='Fried Rice';

这应该是正确答案吗

SELECT AVG(c.age)
FROM customer c
WHERE c.passport EXISTS IN(
SELECT DISTINCT b.passport
FROM breakfast b
WHERE b.item='Fried Rice');

2 个答案:

答案 0 :(得分:2)

如果我在一天早上见到泰勒斯威夫特的早餐(a),你会想象会发生什么?但我真的饥饿并且决定有两个帮助?

想象一下第一个查询返回的行:

paxdiablo   50
paxdiablo   50
taylorswift 26

显然,paxdiablotaylorswift的平均年龄为(50 + 26) / 2 = 38,但平均那些三个行会为您提供(50 + 50 + 26) / 3 = 42

这里的关键是短语"顾客的平均年龄",这意味着一个人吃炒饭的方式并不重要,他们'仍然只是一个客户。

第二个查询确保客户不会在结果中重复,至少在您修复错误使用exists in之后:

create table customer  (name varchar(20), passport varchar(20), age int);
create table breakfast (passport integer, item varchar(20));

insert into customer(name,passport,age) values ('paxdiablo',1,50);
insert into customer(name,passport,age) values ('taylorswift',2,26);

insert into breakfast (passport,item) values (1,'fried rice');
insert into breakfast (passport,item) values (1,'fried rice');
insert into breakfast (passport,item) values (2,'fried rice');

select * from customer;
select "";
select * from breakfast;
select "";

select avg(c.age)
  from customer c, breakfast b
  where c.passport=b.passport
  and b.item='fried rice';

select avg(age)
  from customer
  where passport in (
    select distinct passport
      from breakfast
      where item='fried rice'
  );

drop table customer;
drop table breakfast;

运行该脚本会显示差异:

sh-4.2# sqlite3 mydb.sdb <mysql.sql
paxdiablo|1|50
taylorswift|2|26

1|fried rice
1|fried rice
2|fried rice

42.0
38.0

(a)是的,好像 可能发生: - )

答案 1 :(得分:0)

你的第二个查询有正确的想法,因为泰勒斯威夫特提到的日期,但它是错误的。您可以使用WHERE column in (list of values)WHERE EXISTS (query or condition)

SELECT AVG(c.age)
FROM customer c
WHERE EXISTS (SELECT 1
              FROM breakfast b
              WHERE b.item='Fried Rice'
                AND b.passport = c.passport)
;

或者:

SELECT AVG(c.age)
FROM customer c
WHERE c.passport IN (SELECT DISTINCT b.passport
                     FROM breakfast b
                     WHERE b.item='Fried Rice')
;