Reply
This simple joing mysql select return many duplicate results
Old 10-27-2009, 02:39 PM This simple joing mysql select return many duplicate results
Average Talker

Posts: 27
Name: Mahmoud M. Abdel-Fattah
Location: Alexandria, Egypt.
Trades: 0
I've problem with this query :
http://pastebin.com/d41a87b78 ,

It returns many duplicated results ,
here's image : http://img26.imageshack.us/i/mysqlun.jpg/
m_abdelfattah is offline
Reply With Quote
View Public Profile Visit m_abdelfattah's homepage!
 
 
When You Register, These Ads Go Away!
Old 10-27-2009, 03:53 PM Re: This simple joing mysql select return many duplicate results
chrishirst's Avatar
Super Moderator

Posts: 22,222
Location: Blackpool. UK
Trades: 0
maybe you should be using a LEFT JOIN or possibly a RIGHT JOIN or an INNER JOIN or an OUTER JOIN, difficult to guess at without knowing the table schemas etc.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-27-2009, 06:10 PM Re: This simple joing mysql select return many duplicate results
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It's totally logical...
What you ask the DB, is to return you the fields from the tables transactions, transaction_details, product_items and branches, but you don't tell the db how to match the records from all those tables.

This imply an cross join, as know as a cartesian product.
In short, each row of each table is matched with every rows of all the other tables.
Result: probably a couple of millions of rows.

2 things to do:
1) Drop the "select x from tbl1, tbl2, tbl3" syntax. It's hard to read and to understand.
Use the sql92 syntax:
Code:
select
  t.*,
  td.*,
  pi.name as product_name,
  b.name as branc_name
from transactions as t
  inner join transaction_details as td on td.transactionId=t.transaction
  inner join product_items as pi on td.productId=pi.productId
  inner join branches as b on pi.branchId=b.branchId
where t.retailer_id=1
and t.transaction_id=7
Of course, I have extrapolated the link between your tables here, but you get the idea.
This is the currently admited best practice when it come to write an sql query.

2) use joins:
You have basically 3 types of joins:
* cross join => each rows of each tables are matched to every rows of the other. Basically, you never want this, except in rare cases, where you specifically know what you are doing.

* inner join => the rows of the first table are matched with the rows of the second table on the condition expressed by the "on t1.id=t2.id"
Only records having such a relation are shown. If a record cannot be matched between the 2 tables, it's excluded from the resultset

* outer join => like the inner join, except that you can specify 1 of the 2 tables (the left, ie the first or the right, ie the second) as a reference table.
The matching is done on the "on t1.id=t2.id" relation too, but if a record from the reference table doesn't have a match with the second one, it's included in the result nontheless.

For example, in the query up here.
If each articles have a branch, then the inner join is ok, because every articles will be returned.
But if some articles don't have a branch, then those would not be displayed if we use an inner join.
We would have to use an outer join to do so.
The reference table would be the article table, so the qery would be rewrote:
Code:
select
  t.*,
  td.*,
  pi.name as product_name,
  b.name as branc_name
from transactions as t
  inner join transaction_details as td on td.transactionId=t.transaction
  inner join product_items as pi on td.productId=pi.productId
  left outer join branches as b on pi.branchId=b.branchId
where t.retailer_id=1
and t.transaction_id=7
left outer join, because the table articles is defined before the table branches.
With that query, every articles would be returned, even when no branches are defined for them.

Just be vigilant that you don't mix "inner join" and "outer join" when you write the queries.
The "outer join" should always be the last. Do not ever define an "inner join" after an "outer join". The db engine should be smart enough to rewrite the query internally to avoid this, but you never know...
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 10-27-2009 at 07:30 PM..
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 10-27-2009, 06:50 PM Re: This simple joing mysql select return many duplicate results
Average Talker

Posts: 27
Name: Mahmoud M. Abdel-Fattah
Location: Alexandria, Egypt.
Trades: 0
Ohh @tripy, You Surprised me !
Thanks a lot for your help
m_abdelfattah is offline
Reply With Quote
View Public Profile Visit m_abdelfattah's homepage!
 
Reply     « Reply to This simple joing mysql select return many duplicate results
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.12461 seconds with 13 queries