当数据库中只有一个条目时,SQL Query将返回重复项

时间:2012-03-09 13:16:16

标签: php mysql sql

我正在尝试使用测试数据库来学习SQL,(MySQLi PHP)

我从两个表add_images和item中提取数据,其中'item'的主键被引用为'add_images'中的外键。一个'项目'只有一个图像(add_images)。

我需要获得有关它的image_name引用的项目的具体细节。

我在d7.jpg下的数据库中只有一个条目,类型为VARCHAR。当我运行以下查询时,我得到34个结果,我应该只得到一个?这是为什么?

当我在phpmyadmin

中运行查询时,也会发生这种情况

这是查询

SELECT item_name, catagory, brand, store, location, month, year, details FROM add_images, item WHERE add_images.image_name='d7.jpg '

我认为我在配置表格的方式上做了一些基本错误的事情?

希望有人可以提供建议!

由于

下面是两个表

CREATE TABLE IF NOT EXISTS `add_images`( `image_id` int(10) unsigned NOT NULL     AUTO_INCREMENT COMMENT 'unique id for an image',
  `item_id` int(10) unsigned NOT NULL COMMENT 'unique id for the item been added',
  `image_name` varchar(20) NOT NULL COMMENT 'name of the image',
  `type` enum('standard','deleted','profile','look','item') NOT NULL COMMENT 'status and       type of image',
  `date_added` varchar(50) NOT NULL COMMENT 'date image was added',
  PRIMARY KEY (`image_id`),
  UNIQUE KEY `item_id` (`item_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='Add images to item or profile picture'     AUTO_INCREMENT=50 ;


CREATE TABLE IF NOT EXISTS `item` (
 `item_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key for item',
 `item_name` varchar(20) NOT NULL COMMENT 'title name of the item',
 `catagory` enum('accessories','jackets','coats','footwear','legwear','jeanswear','dresses','shirts','tops','t-shirts','knitwear','skirts','shorts') NOT NULL COMMENT 'item catagory',
 `brand` varchar(20) NOT NULL COMMENT 'brand of product',
 `store` varchar(20) NOT NULL COMMENT 'store the item was purchased',
 `location` varchar(20) NOT NULL COMMENT 'location the item was purchased',
 `month` enum('January','February','March','April','May','June','July','August','September','October','November','December') NOT NULL COMMENT 'month the item was purchased',
 `year` int(2) NOT NULL COMMENT 'year the item was purchased',
 `details` varchar(500) NOT NULL COMMENT 'details about the item description',
 `date` varchar(50) NOT NULL COMMENT 'date item created',
 PRIMARY KEY (`item_id`),
 UNIQUE KEY `item_id` (`item_id`)
)    ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='holds data about the item'     AUTO_INCREMENT=72 ;

3 个答案:

答案 0 :(得分:2)

您的查询执行连接但未在item表的WHERE子句中放置任何条件,因此查询将所有 item条目与add_images结果组合在一起无论item_id是否匹配。您需要更改查询以反映该内容;

SELECT item_name, catagory, brand, store, location, month, year, details 
FROM add_images, item 
WHERE add_images.image_name='d7.jpg '
  AND item.item_id = add_images.item_id;

编辑:如果您将查询重写为显式连接,则更容易看出您是否错过了表之间的链接;

SELECT item_name, catagory, brand, store, location, month, year, details 
FROM add_images
JOIN item ON item.item_id = add_images.item_id   -- ON specifies the link
WHERE add_images.image_name='d7.jpg ';

答案 1 :(得分:1)

这是因为您正在执行JOIN(在FROM中隐含地使用2个表)而没有指定它们之间的链接。

好的语法是:

SELECT item_name, catagory, brand, store, location, month, year, details FROM add_images, item WHERE add_images.image_name='d7.jpg ' AND add_images.item_id=item.item_id

答案 2 :(得分:1)

您需要加入条件:

SELECT item_name, catagory, brand, store, location, month, year, details 
FROM add_images, item 
WHERE add_images.item_id = item.item_id and add_images.image_name='d7.jpg '

- 使用SQL-89语法,或:

SELECT item_name, catagory, brand, store, location, month, year, details 
FROM add_images
JOIN item ON add_images.item_id = item.item_id 
WHERE add_images.image_name='d7.jpg '

- 使用SQL-92语法。