表格:
Processor(_pid_, brand, clockSpeed, supplierCity)
Memory(_mid_, brand, capacity, supplierCity)
Harddrive(_hdid_, brand, capacity, RPM, supplierCity)
PC(_pcid_, _mid_, _pid_, _hdid_, model, price, cost)
我正在尝试构建一个查询:
Find's the PC model whose price is less than 1000 pounds and for which
the memory and the processor are supplied from the same city
我的当前查询返回多次重复相同的结果:
SELECT pc.model FROM pc, memory, processor WHERE
memory.supplierCity=processor.supplierCity AND pc.price<1000;
Change the sender name php mail instead of sitename@hostname.com
答案 0 :(得分:0)
SELECT *
FROM pc
WHERE price < 1000
AND EXISTS
(
SELECT NULL
FROM processor p
JOIN memory m
USING (supplierCity)
WHERE (p._pid_, m._mid_) = (pc._pid_, pc._mid_)
)
答案 1 :(得分:0)
正如vkp在评论中所说的那样,尽量避免使用逗号表示法(有用的情况,而不仅仅是懒惰的情况通常是简单而罕见的)。另外,像vkp所说,pc
没有加入,如下所示。
SELECT pc.model
FROM pc
INNER JOIN memory ON pc.`_mid_` = memory.`_mid_`
INNER JOIN processor ON pc.`_pid_` = processor.`_pid_`
WHERE pc.price<1000
AND memory.supplierCity=processor.supplierCity
;
AND
部分可以移动到第二个ON
子句,根据您的数据可能更有效;但从概念上讲,这更容易解释。
它获取所有pc记录及其相应内存和处理器的列表,然后将结果保存在pc价格低于1000且内存和处理器由同一城市提供的位置。但是,由于MySQL通常会处理查询,因此很可能首先获得低于1000的定价pc条目列表,然后找到每个条目的内存和处理器。