坚持使用Mysql查询

时间:2014-11-26 22:03:34

标签: mysql

我希望查询返回与我的搜索条件匹配的所有项目,但是当表格返回时,会有相同项目的重复项。

SELECT i.*
FROM Item i, Lead_Singer s
WHERE i.category = '' OR i.title = 'test 2 CD' OR (s.name = '' AND i.upc = s.upc);

结果类似于

10 test 2 CD CD Rock Easy Entertainment 2013 19.99 11

10 test 2 CD CD Rock Easy Entertainment 2013 19.99 11

10 test 2 CD CD Rock Easy Entertainment 2013 19.99 11

10 test 2 CD CD Rock Easy Entertainment 2013 19.99 11

符合描述的所有项目将被退回4次。

我的表是:

CREATE TABLE item(
upc char(30) not null,
Title char(30) not null,
Type enum("CD" , "DVD") not null,
Category enum("Rock", "Pop", "Rap", "Country", "Classical", "New age", "Instrumental") not null,
Company char(30) not null, 
Year year not null,
Price char(30) not null, 
Stock char(30) not null,
PRIMARY KEY(upc));

CREATE TABLE Lead_Singer(
upc char(30) not null, 
Name char(30) not null,
PRIMARY KEY(upc, name));

ALTER TABLE lead_singer
ADD FOREIGN KEY(upc) REFERENCES item(upc);

任何提示将不胜感激!

1 个答案:

答案 0 :(得分:1)

选择不同只会为您提供不同的结果

SELECT DISTINCT i.*
FROM Item i, Lead_Singer s
WHERE i.category = '' OR i.title = 'test 2 CD' OR (s.name = '' AND i.upc = s.upc);
相关问题