Jump to content

Multi-Table Query Returning Duplicate Results


nodirtyrockstar

Recommended Posts

Can anyone help me understand why the following query:

 

SELECT products.id, merch.id FROM products, merch;

 

Would return the results from the product table two times, and the results from the merch table three times?

 

Right now there is just dummy content in my tables (two products in one, and three in the other), and there is no duplication. Let me know if you need more information. I am sure (hoping) it is something simple that I am doing wrong.

Link to comment
Share on other sites

Without a JOIN, that query will return a Cartesian product. Every row in "products" will be matched with every row in "merch". You need to specify how the tables are related:

 

SELECT products.id, merch.id
FROM products JOIN merch ON products.preferred_merchant_id = merch.id

 

Of course the columns I used in the above query are just examples, the actual JOIN condition would depend on the structure of your tables.

Link to comment
Share on other sites

Merch contains one type of product, and products contains another type of product. (I need to rename it, I know...)

 

CREATE TABLE `products` (\
`img` varchar(20) NOT NULL default '../img/albumArt2.gif',
`id` varchar(20) NOT NULL, 
`artist` varchar(30) NOT NULL,
`title` varchar(30) NOT NULL,
`label` varchar(30) NOT NULL,
`year` year(4) NOT NULL,
`price` decimal(11,2) unsigned NOT NULL,
`qty` int(11) unsigned NOT NULL,
`desc` varchar(255) NOT NULL,
`agedOff` tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TABLE `merch` (
`img` varchar(20) NOT NULL default '../img/albumArt2.gif',
`id` varchar(20) NOT NULL,
`title` varchar(30) NOT NULL,
`size` varchar(3) NOT NULL,
`color` varchar(10) NOT NULL,
`sex` varchar(6) NOT NULL default 'Unisex',
`price` decimal(11,2) unsigned NOT NULL,
`qty` int(11) unsigned NOT NULL,
`desc` varchar(255) NOT NULL,
`agedOff` tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1

What I would like to do is just get a list of all the product IDs in both tables.

Edited by nodirtyrockstar
Link to comment
Share on other sites

You need a UNION. The layouts are different so you will need to specify which ones you need as the corresponding columns in each part of the union need to be of the same type

 

eg

 

SELECT price, qty, description, artist FROM products
UNION
SELECT price, qty, description, NULL FROM merch

 

merch table does not have artist so NULL substituted in this example so the two parts are the same

Link to comment
Share on other sites

If there is a possibility of identical rows(same ids) from both selects, UNION will only show one of them unless you use UNION ALL. If you are just doing ids I'd do it this way so you know which table they belong to

SELECT id, 'P' as tablename FROM products
UNION
SELECT id, 'M' as tablename FROM merch
ORDER BY id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.