Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query returns duplicates when there is only one entry in the database

Tags:

sql

php

mysql

I am experimenting with a test database to learn SQL, (MySQLi PHP)

I am pulling data from two tables add_images and item where the primary key for 'item' is referenced as the foreign key in 'add_images'. One 'item' will only has one image (add_images).

I need to get specific details about an item referenced by it's image_name.

I only have one entry in the database under d7.jpg which is of type VARCHAR. When I run the below query I get 34 results where I should only get one? why is this?

This also happens when I run the query in phpmyadmin

Here is the query

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

I think I am doing something fundamentallly wrong here in the way I have my tables configured?

Hope someone can advise!

Thanks

below are the two tables

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 ;
like image 628
Alan Avatar asked Dec 04 '25 11:12

Alan


2 Answers

Your query does a join but does not put any condition in the WHERE clause on the item table, so the query will combine all item entries with the add_images result no matter if the item_id matches. You'll want to change your query to reflect that;

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;

EDIT: If you rewrite the query as an explicit join, it's easier to see if you're missing a link between the tables;

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 ';
like image 51
Joachim Isaksson Avatar answered Dec 07 '25 01:12

Joachim Isaksson


it's because you are performing a JOIN (implictly using 2 tables in the FROM) without specifying the link between them.

The good syntax would be:

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
like image 22
dweeves Avatar answered Dec 07 '25 01:12

dweeves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!